I was looking for an example of how to use a specific part of the Telerik grid mesh (ASP.NET MVC3, but this is really not relevant here). They have a piece of code that takes a list of filter descriptions and builds an expression:
System.Linq.Expressions.Expression<Func<MyModel, bool> exp = ExpressionBuilder.Expression<MyModel>(listOfFilters);
Ok, I think everything is fine. Expression wraps a lambda that works with MyModel to create a bool. Fine. Now their example just drops out, for example, in "Where":
someList = someList.Where(exp);
I assume that it should be โheyโ to apply this expression to all the elements in the list (which, of course, is a common MyModel). However, VS claims that the code does not compile. I get "No overload Where is or System.Func has a few invalid arguments."
I played with him and found that I could compile an expression that gives a more indecent look
someList = someList.Where(x => exp.Compile()(x));
Which compiles and probably works, but it becomes uncomfortable for me, because now I work clearly beyond what I know.
Is there a reason why (setting the IDE, flag, outdated documentation) the sample approach does not work? Is there an approximate equivalence of my hack and an example? Do I have to structure this hack differently in order to avoid any disgusting problem (for example, not going to compile the expression every time it checks an item in a list, right? I think it is smart enough for that?)
- Edit Yes, it was IEnumerable. I ended up in "all wheres created equal" traps. Thanks everyone!