Dynamic expression tree how to
- by Savvas Sopiadis
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