Dynamic String based Queries in LINQ - Dynamic Expression API - Raj Kaimal

Dynamic String based Queries in LINQ - Dynamic Expression API

Version: Visual Studio 2008 Beta 2

The Dynamic Expression API extends the core LINQ API with the ability to dynamically create string based queries that are constructed at run-time. The API provides

  • Dynamic parsing of strings to produce expression trees (the ParseLambda and Parse methods),
  • Dynamic creation of “Data Classes” (the CreateClass methods), and
  • Dynamic string-based querying through LINQ providers (the IQueryable extension methods).

Assuming I have created a LINQ to SQL entity model for the NorthWind database in VS 2008,

 1: NorthwindDataContext context = new NorthwindDataContext();

I normally would write a query to return all customers ordered by the ContactName in descending order like so:

 1: var x = context.
 2: Customers.
 3: OrderByDescending(c => c.ContactName);

If instead, I wanted to define the Sort column and Sort direction in a string, I could rewrite the query, with the help of the Dynamic Expression API, like so:

 1: string sortExpression = "ContactName DESC";
 2: var x1 = context.
 3: Customers.
 4: OrderBy(sortExpression);

The SQL generated in both cases will be

 1: SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], 
 2: [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
 3: FROM [dbo].[Customers] AS [t0]
 4: ORDER BY [t0].[ContactName] DESC

Moving on to another example, If I had a query like so,

 1: DateTime myDate = Convert.ToDateTime("7/26/1996");
 2: var y = context.
 3: Orders.
 4: Where(a => (a.Customer.Country == "Switzerland") && (a.OrderDate < myDate)).
 5: OrderBy(o=> o.OrderDate).
 6: Select(o => new { o.Customer.ContactName, o.Customer.Country, o.OrderDate });

I could use the Dynamic Expression API to rewrite it like this:

 1: var y1 = context.
 2: Orders.
 3: Where("Customer.Country == @0 and OrderDate < @1", "Switzerland", myDate).
 4: OrderBy("OrderDate").
 5: Select("new(Customer.ContactName,Customer.Country, OrderDate)");

Note that, in the query above, the shape of the result is specified as a string. Note also that I have defined what are known as substitution values in the Where statement.

The SQL generated in both cases would be:

 1: exec sp_executesql N'SELECT [t1].[ContactName], [t1].[Country], [t0].[OrderDate] 
 2: FROM [dbo].[Orders] AS [t0]
 3: LEFT OUTER JOIN [dbo].[Customers] AS [t1] ON [t1].[CustomerID] = [t0].[CustomerID]
 4: WHERE ([t1].[Country] = @p0) AND ([t0].[OrderDate] < @p1)
 5: ORDER BY [t0].[OrderDate]'
 6: N'@p0 nvarchar(11),@p1 datetime',
 7: @p0=N'Switzerland',
 8: @p1='1996-07-26 00:00:00:000'

To use the Dynamic Expression API, add the Dynamic.cs class to your project and import the System.Linq.Dynamic namespace.  

To learn more, download the LINQ and language samples for Visual Studio 2008 Beta 2 zip file located at the Visual Studio 2008 samples page.

Published Tuesday, September 18, 2007 12:21 AM by rajbk
Filed under: , ,

Comments

# re: Dynamic String based Queries in LINQ - Dynamic Expression API

I created a similar framework on codeplex at http://www.codeplex.com/nlinq. The advantage is that it works on VS.NET 2003 & 2005.

Tuesday, September 18, 2007 2:23 AM by S&#233;bastien Ros

# re: Dynamic String based Queries in LINQ - Dynamic Expression API

Hi,Raj, Where can I download the Dynamic.cs  file? I can't find it.

Monday, October 29, 2007 8:53 AM by cokkiy

# re: Dynamic String based Queries in LINQ - Dynamic Expression API

Cokkiy,

Go to the samples page at the bottom of the post.

Monday, October 29, 2007 9:47 AM by rajbk

# LINQ : Paging and sorting LINQ Queries with a custom QueryDataSource

So you've got a LINQ query and now you want to drop it on a GridView. If you assign it to the datasource

Saturday, November 03, 2007 3:56 AM by Community Blogs

# LinqDataSource exceptions

Prerequisite: LinqDataSource &amp; SqlDataSource Master/Details When working with the LinqDataSource

Thursday, January 31, 2008 9:10 PM by Raj Kaimal

# re: Dynamic String based Queries in LINQ - Dynamic Expression API

This is for DLinq how about linq?

Wednesday, March 26, 2008 10:57 PM by Ricky

# re: Dynamic String based Queries in LINQ - Dynamic Expression API

I'm having trouble getting this to work with LINQ TO XML

It seems the dynamic namespace only extends the IQuereyable and not the IEnumerable.

Anybody managed to make this work with XML as the Data Source provider?

Friday, May 23, 2008 5:53 AM by Shereef

Leave a Comment

(required) 
(required) 
(optional)
(required)