Expression.OrElse, dynamically creating a condition
I am attempting to create a dynamic where clause using the standard expression API.
var query = (
from 开发者_开发技巧p in Parties
orderby p.PartyId
orderby p.FullName
select p
).AsQueryable();
Expression<Func<Party, bool>> @fn = (p) => SqlMethods.Like(p.FullName, "%smith%") || SqlMethods.Like(p.Person.FirstName, "%smith%");
Expression<Func<Party, bool>> @sn = (p) => SqlMethods.Like(p.Person.FirstName, words[0]);
ParameterExpression pe = Expression.Parameter(typeof(Party), "p");
Expression orelse = Expression.OrElse(
Expression.Lambda(@fn, pe),
Expression.Lambda(@sn, pe)
);
The expressions above will ultimately be added to a where clause. I need to add a bunch of 'likes'.
How do I do this?
I get InvalidOperationException on the operator OrElse
I have also tried Expression.Or
Thanks
Regards
Craig.
Have you checked out PredicateBuilder?
http://www.albahari.com/nutshell/predicatebuilder.aspx
It can make dynamically creating expressions for where clauses much easier.
精彩评论