Dynamic expression tree how to

Posted by Savvas Sopiadis on Stack Overflow See other posts from Stack Overflow or by Savvas Sopiadis
Published on 2010-03-19T10:40:16Z Indexed on 2010/03/19 10:41 UTC
Read the original article Hit count: 279

Hello everybody!

Implemented a generic repository with several Methods. One of those is this:

public IEnumerable<T> Find(Expression<Func<T, bool>> where)
        {
            return _objectSet.Where(where);
        }

Given to be it is easy to call this like this:

Expression<Func<Culture, bool>> whereClause = c => c.CultureId > 4 ;

return cultureRepository.Find(whereClause).AsQueryable();

But now i see (realize) that this kind of quering is "limiting only to one criteria". What i would like to do is this:

in the above example c is of type Culture. Culture has several properties like CultureId, Name, Displayname,... How would i express the following: CultureId > 4 and Name.contains('de') and in another execution Name.contains('us') and Displayname.contains('ca') and ....

Those queries should be created dynamically. I had a look in Expression trees (as i thought this to be a solution to my problem - btw i never used them before) but i cannot find anything which points to my requirement.

How can this be costructed?

Thanks in advance

© Stack Overflow or respective owner

Related posts about lambda-expressions

Related posts about expression-trees