LINQ (Language INtegrated Query) is one of the most powerful new features coming with C# 3.0, that allow to compose queries and access data through your programming language. LINQ has different benefits, and one of these is, for sure, to write type-safe queries using C# or Visual Basic (also with the help of the Visual Studio Intellisense).
But, there is always a “but”, sometimes also the coolest feature cannot be what we’re actually looking for.
One drawback on working with Linq could be that you need to build your query dinamically… but you can’t.
I was searching a bit around the net and in the C# 4.0 documentation/samples, I found a nice set of extension methods that allow Linq to overtake this problem: the LINQ Dynamic Query Library (actually searching a little bit better I found that this library was included also in the VS2008 SP1 examples, but this is another story ).
By importing this library in our projects we’ll be able to write a code like this:
and this will work in the same way of the following snippet:
So.. what is the point?
Quite simple… Let’s try to figure out if we need to filter a set of data but we don’t know yet how to do it, maybe cause we need some input from the user, like in most business analysts applications. Using this library we will be able to take advantage of all LINQ’s features and power without loosing flexibility and usability.
Ok, ok I know.. this looks too much general and also a “bit” marketing oriented.
An actual scenario where this library is really useful, it’s when you’re working with the jQuery Grid plugin and a Asp.Net MVC (or Asp.Net website).
and here is the part of the GridExample method where the use of the Linq Dinamiyc Query Library is really useful:
I’m testing now this library and I’m really looking forward to see it integrated in the next LINQ realease.