Tuesday, January 08, 2008

Dynamic LINQ

Scott Gu's Blog - Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)

"...

One of the benefits of LINQ is that it enables you to write type-safe queries in VB and C#.  This means you get compile-time checking of your LINQ queries, and full intellisense and refactoring support over your code:

...

While writing type-safe queries is great for most scenarios, there are cases where you want the flexibility to dynamically construct queries on the fly.  For example: you might want to provide business intelligence UI within your application that allows an end-user business analyst to use drop-downs to build and express their own custom queries/views on top of data. 

Traditionally these types of dynamic query scenarios are often handled by concatenating strings together to construct dynamic SQL queries.  Recently a few people have sent me mail asking how to handle these types of scenarios using LINQ.  The below post describes how you can use a Dynamic Query Library provided by the LINQ team to dynamically construct LINQ queries.

Downloading the LINQ Dynamic Query Library

Included on the VS 2008 Samples download page are pointers to VB and C# sample packages that include a cool dynamic query LINQ helper library.  Direct pointers to the dynamic query library (and documentation about it) can be found below:

Both the VB and C# DynamicQuery samples include a source implementation of a helper library that allows you to express LINQ queries using extension methods that take string arguments instead of type-safe language operators. 

...

Using the LINQ DynamicQuery library I could re-write the above query expression instead like so:

step2

..."

I really need to start spending some quality time with LINQ...

No comments: