posted by: Ralf Rottmann | posted @ Sunday, November 25, 2007 11:45 PM | View blog reactions

In many LINQ (to SQL) related posts questions regarding the dynamic construction of LINQ queries arise. Most of the available answers deal with using LINQ's Expression classes to construct an expression tree at runtime. While this definitely is an elegant way, it's also extremely difficult for most object oriented programmers to understand. I'd therefore provide a little bit of an easier solution which simply constructs the where constraint from a string.

I admit that this takes away some of LINQ's type safeness and beauty and goes closely to creating an SQL command on the fly, however, it's inspired by a post from Anders Hejlsberg, the inventor of LINQ, so I assume it's not too wrong. ;-)

Here's the problem:

Suppose you've got a list of search terms which might vary at runtime and you want to declare a LINQ to SQL query which returns all Customers from the Nortwhind database, where the ContactName contains any of the search terms.

Here is the LINQ to SQL query that does exactly that:

code

string[] searchTerms = new string[] { "Maria", "Pedro" };

string search = "";

 

foreach (string searchTerm in searchTerms)

{

    search += "ContactName.Contains(\""+searchTerm+"\") or ";

}

 

search += "1=0";

 

var query = db.Customers.Where(search).Select(c => c);

 

It's very straight forward: In the foreach loop we are concatenating a string which will serve as the condition for the where clause. In the above example this will evaluate to:

ContactName.Contains(\"Maria\") or ContactName.Contains(\"Pedro\") or 1=0

The "1=0" is only there because otherwise search would end with an "or" operator once the loop finishes and LINQ would not be able to parse it.

Before you copy and try to compile: Obviously the shipping version of LINQ to SQL does not support any overload for the Where extension method, that takes a single string parameter as its argument. In order to make the above compile you have to download and include further extension methods for LINQ from Microsoft. Once you've downloaded expand the archive and include the System.Linq.Dynamic namespace contained within the Dynamic.cs file LinqSamples\DynamicQuery\DynamicQuery.

I wondered why these methods did not make it into the final shipping build of LINQ but am happy that I finally found them!

Hope this helps!

 

comments
Golo Roden stated:
# re: dynamic linq queries / dynamic where clause (part 1)
Hey Ralf,

thanks for your post ... I guess, now my current main LINQ problem has been solved finally :-)!

Thanks a lot,


Golo
posted on 11/26/2007 10:06 AM
24100.net stated:
# dynamic linq queries / dynamic where clause (part 2)
dynamic linq queries / dynamic where clause (part 2)
posted on 11/26/2007 10:48 AM
Chuck Snyder stated:
# re: dynamic linq queries / dynamic where clause (part 1)
While adding " or 1=0"
it seems a little more elegant to just do a substring on the search??

search = search.substring(0,search.length-3)

chuck
posted on 4/29/2008 11:12 PM
Fakeer Mohamed stated:
# re: dynamic linq queries / dynamic where clause (part 1)
Hi Ralf,
Thank You very much. it's working fine.
posted on 8/30/2008 8:39 AM
Frank Neubecker stated:
# re: dynamic linq queries / dynamic where clause (part 1)
Ralf,

Didn't think "Contains..." could be put in a string like that.

Thanks much,
Frank
posted on 9/10/2008 12:23 AM
Dioni stated:
# re: dynamic linq queries / dynamic where clause (part 1)
Couldn't you have done it too like this:

string[] searchTerms = new string[] { "Maria", "Pedro" };

var query = from c in db.Customers where(searchTerms.Contains(c.ContactName) select c;
posted on 10/7/2008 4:52 PM
post your comment
Title *
Name *
Email
Url
Comment *  
Please add 6 and 3 and type the answer here: