5

I recently stumpled upon the problem to dynamically create Linq expressions during runtime. Most examples I found deal with the rather simple task of just comparing one property of a given database entity with a single parameter. Like so:

 Session.Query.Where(m => m.Name.Contains("test"))

Which could also be achieved with a far more generic approach like this:

var item = Expression.Parameter(typeof (MyClass), "item");
var property = Expression.Property(item, "Name");
var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var searchExpression = Expression.Constant(searchString, typeof(string));
var containsMethodExpression = Expression.Call(property, containsMethod, searchExpression);
var lambda = Expression.Lambda<Func<MyClass, bool>>(containsMethodExpression, item);
query = query.Where(lambda);    

However, sometimes the task is somewhat more complex and one wants to attain something like the following:

Session.Query.Where(m => m.SpecialProperty.Any(f => f.Name.Contains("test")));

Where "SpecialProperty" is of the type List<> and the property "Name" is of the type string.

Is it possible to build a Linq expression like this dynamically and how could this be achieved? Are there any performance concerns regarding this approach?

UnclePaul
  • 515
  • 5
  • 17

2 Answers2

6

Found a solution that is working in my particular use case and does exactly what I was looking for.

/* 
building expression tree 
example: Session.Query.Where(m => m.SpecialProperty.Any(f => f.Name.Contains("test")))
*/ 

var innerItem = Expression.Parameter(typeof(MyInnerClass), "f");
var innerProperty = Expression.Property(innerItem, "Name");
var innerMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var innerSearchExpression = Expression.Constant(searchString, typeof(string));
var innerMethodExpression = Expression.Call(innerProperty, innerMethod, new[] { innerSearchExpression });
var innerLambda = Expression.Lambda<Func<MyInnerClass, bool>>(innerMethodExpression, innerItem);

var outerItem = Expression.Parameter(typeof(MyOuterClass), "m");
var outerProperty = Expression.Property(outerItem, info.Name);
/* calling a method extension defined in Enumerable */
var outerMethodExpression = Expression.Call(typeof(Enumerable), "Any", new[] { typeof(MyInnerClass) }, outerProperty, innerLambda);
var outerLambda = Expression.Lambda<Func<MyOuterClass, bool>>(outerMethodExpression, outerItem);
query = query.Where(outerLambda);

This rather dowdy approach is needed instead of the more elegant single line LINQ-Expression to allow for parametrization of types and method names. However, I of course wouldn't mind other suggestions and ideas on possible performance penalties.

It is very likely that this code snippet could also assist in solving How to produce a Subquery using non-generic Lambda.

Community
  • 1
  • 1
UnclePaul
  • 515
  • 5
  • 17
2

I recommend you look at these links:

Joseph and Ben Albahari's PredicateBuilder

The LINQ dynamic query library

I haven't used either for a while, but one or the other should help.

Phil
  • 42,255
  • 9
  • 100
  • 100