Almost two years ago ScottGu posted some really awesome code that lets you create dynamic where clauses in Linq - meaning you could use a string builder to create your where clause just like the good 'ole days when ad-hoc sql was all the rage. You're going to be better off if you can build a predicate using lambdas, but sometimes its a real pain the the rear to build a proper predicate (Func<T, bool> or Predicate<T>). Building a predicate properly typically becomes difficult when you're giving your users some mechanism to build their own filter for their data.

The problem I had with ScottGu's code is that the extension methods only worked off IQueryable<T>, and well, sometimes you would like to create a dynamic where clause for a datasource that didn't come from an IQueryProvider like LinqToSql. Sometime you want to create a dynamic where clause for a plain old IEnumerable<T>.

So, I took the time to dig through ScottGu's pretty complicated code and created a handful of methods that help you generate useful dynamic functions - including dynamic "OrderBy" and "Where" extension methods for IEnumerable<T>.

I've attached a single common file that includes both ScottGu's code, as well as my own.

Download my code here:
DynamicLinqExtensions.zip (15 KB)

Examples:
IEnumerable<Person> allPeople = GetAllPeople();
foreach(var person in allPeople.Where("Age > 10").OrderBy("Age"))
{
//notice the above arguments are strings and not lambdas
//this makes it a lot easier to build everything ad-hoc
Console.WriteLine(person.Age + " - " + person.FirstName);
}
I also included a method called CreateValueExpression, that will evaluate an expression on any object and return the result.

var fullName = DynamicLinqExtensions.CreateValueExpression<Person,string>("First + \" \" + Last");
//outputs "Jon von Gillern"

Console.WriteLine(fullName(new Person { First = "Jon", Last = "von Gillern" }));


You can find the Gu's original blog post here:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

As always, Kicks and Shoutouts are appreciated!


Wednesday, November 4, 2009 - 7:21 AM CST - Permalink - Comments [2]
Tags: Tips and Tricks


Thursday, November 05, 2009 7:02:41 AM (Central Standard Time, UTC-06:00)
Cool... I am going to have to check these extensions out.

I worked on a Silverlight app where I used the dynamic linq library along with ADO.NET DataServices to do some complex list filtering on the client. It worked great! I did have to make a small modification to run the dynamic linq library in silverlight (I think it had to do with a reader writer lock that was not supported in silverlight).

Thanks for posting this!

...Ed
Monday, November 09, 2009 2:21:41 PM (Central Standard Time, UTC-06:00)
I created my own ExpressionEval (http://expressioneval.codeplex.com/) compared to dynamic linq library it is more like 5 times faster and has also linq support.
Comments are closed.
Nitriq is a Code Analysis Tool for .Net Developers. It helps you visualize your code and quickly find types and methods that need refactoring.
It is currently in a free public beta, but will have reasonable prices once the bugs are worked out.
I'll be using this blog to talk about .Net, C#, WPF, ASP.NET, Nitriq and MicroISVs.


RSS
Tags

Archive



Download   |   Documentation   |   Instructions   |   Support   |   Blog   |   About