Something I just saw for the first time is a Microsoft C# sample application called DynamicQuery. This allows you to enter part of a LINQ query as a string.
If you have Visual Studio 2008 installed, you can find the samples in this directory: C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033. Then extract the file CSharpSamples.zip. The sample will be found at CSharpSamples\LinqSamples\DynamicQuery.
From the sample you will see that you can do perform things like this:
var query = db.Customers.Where(
"City == @0 and Orders.Count >= @1", "London", 10). OrderBy("CompanyName"). Select("New(CompanyName as Name, Phone)");
All of the heavy lifting happens in the Dynamic.cs file that has 11 classes. The interface happens through DynamicQuery static class, which extends the IQueriable interface with new Where, OrderBy, and Select methods. To use these extensions you have to include this line in your code: using System.Linq.Dynamic; along with the Dynamic file in your project.
I don’t have a use for this quite yet, but I think its time will come.
Linq is just too cool.
I just used this for a practical reason yesterday. I created a custom GridView called QueryableGridView that works with IQueryable<T>s without using the limiting LinqDataSource. The trick I needed to solve was handling paging and sorting against any IQueryable without having to know any details about it.
The following bit of code, courtesy of Dynamic Linq works like a charm.
int count = query.Count(); // need this to fill the pager.
if(AllowSorting)
sortedQuery = query.OrderBy(SortExpression); // here’s the bit that dynamic linq handles – it converts the SortExpression string to linq.
if(AllowPaging)
pagedQuery = query.Page(PageIndex, PageSize); // an extension method I wrote combining Skip and Take.
…