Expression.OrElse, dynamically creating a condition.

Posted by Jim on Stack Overflow See other posts from Stack Overflow or by Jim
Published on 2010-06-03T09:29:58Z Indexed on 2010/06/03 12:34 UTC
Read the original article Hit count: 281

Filed under:
|

Hi,

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.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about dynamic