Dynamic LINQ

Database applications frequently rely on Dynamic SQL, i.e. queries that are constructed at run-time through program logic. For example, you might want to create a business UI within your application that allows the end users to use drop-down controls to build and express their own custom queries on top of data. Traditionally these types of scenarios are handled by concatenating strings to construct dynamic SQL queries. The Dynamic Expression API allows you to handle such scenarios by using LINQ.

Downloading the LINQ Dynamic Query Library

The dynamic query library can be downloaded as part of the VS 2008 samples, which are available here:

The API is located in the Dynamic source file, which can be found in:

  • For VB - included in the \Language Samples\LINQ Samples\DynamicQuery\DynamicQuery directory. The name of the file is Dynamic.vb.
  • For C# - included in the \LinqSamples\DynamicQuery\DynamicQuery directory. The name of the file is Dynamic.cs.

The Dynamic source file includes a helper library that allows you to express LINQ queries using extension methods that take string arguments instead of type safe operators.

To use the Dynamic Expression API, you could simply copy/paste the Dynamic source file in your project.

Using Dynamic LINQ with Telerik Data Access

The Dynamic LINQ extension methods are brought in to scope by using/importing the System.Linq.Dynamic namespace.

using (FluentModel dbContext = new FluentModel())
   List<Car> cars = dbContext.Cars.Where("CarYear = @0 and ABS = @1", 2006, true).ToList();
Using dbContext As New FluentModel()
 Dim cars As List(Of Car) = dbContext.Cars.Where("CarYear = @0 and ABS = @1", 2006, True).ToList()
End Using

Dynamic LINQ Documentation

Included with the above C# and VB samples is some HTML documentation that describes how to use the Dynamic Expression API.