The Missing Linq

I’ve almost wrapped up porting my ExtJS demo application to ASP.NET. It has been pretty simple so far once I realized I was trying to make it too complex. After getting a handle on ASP.NET MVC, I realized I didn’t need to use WCF for the JSON calls. ASP.NET MVC is pretty slick and has excellent built-in support for returning JSON data via the JsonResult class and Json method.

For the data, I’m using a very simple database I called Demo which contains two tables, City and Country. They look like this:

I’m using Linq to SQL for data access, and everything was moving along quite nicely until I ran into a small problem. In the Java version of my demo application, I used server-side sorting for the grid. How this works is that when you click a column header on the ExtJS Grid, the data store makes an AJAX call to the server controller with the POST data containing two parameters. The sort parameter contains the name of the column in the data store that the sort should be based on. The dir parameter has either the world ASC or DESC, indicating the direction of the sort.

The method signature in my controller which catches this request looks like this:

ASP.NET MVC will automatically map the two POST parameters to the two parameters in the method, since they have the same names. Where I hit a wall though was with Linq.

One of the big design features of Linq is to provide a strongly typed interface for the query language. Since I wanted the orderby statement to be based on the POST parameter, I thought I could do something like this:

Noticing no red squiggles, I assumed this would work. But it doesn’t. The problem is Linq is strongly typed, so it won’t accept a string after orderby. Linq wants something like this:

This would be cool in normal circumstances, but since I need to vary the orderby based on the sort parameter, I would have to do a large if-then block with multiple Linq queries, executing the required one based on the incoming parameter. Since that sounded like a pretty stupid idea, I dug a big deeper.

After much searching, I determined that Linq, as implemented, can’t handle the dynamic queries. But fortunately some other very smart people created a work around in the form of an extension to Linq called the Dynamic Query Library. Scott’s article provides a pretty good introduction. All you need to do is include the Dynamic.cs file in the solution and add the namespace to the imports.

Using the Dynamic Query extension methods, I can now do something like this:

The Dynamic.cs adds a new version of the OrderBy method which takes a string parameter containing the name of the field to sort on and an optional sort parameter. No if-then loops from hell and no more hair pulling trying to find the simplest solution that works.

I would bet the Dynamic.cs stuff ends up in core Linq, as it is simply too valuable for situations requiring dynamic data access, like my ExtJS demo. It also provides similar support for strings to the other Linq commands.

Now that I’ve gotten past the ugliness, I’ll be able to wrap up my ExtJS demo application. I’m adding some additional features to it which demonstrate some of the coolness of ASP.NET MVC, so stay tuned.

4 thoughts on “The Missing Linq”

  1. Tim –

    Glad to see you blogging about integrating Ext with different server-side technologies. I like your idea on the forums about the <a href=”″Ext conference of demonstrating how Ext is platform agnostic and how to integrate with various technologies. You mentioned that you are working on both a Java and a .NET version of your demo app. Does the demo work with the same front-end and just different back-ends or are they 2 completely different apps?

    Aaron Conran
    Ext JS Core Developer

  2. Hi Aaron,
    The front-end JavaScript code containing the Ext grid will be pretty much the same, short of the URLs. In the Spring version, I took advantage of Spring MVC, while in the .NET version I’ll use ASP.NET MVC with a MasterPage. This means the HTML will be a bit different. I’m also adding a master-detail piece to the .NET example and plan to include a “update comment” piece using an Ext form. I’ll then go retrofit the enhancements into the Java/Spring version.

  3. Thanks for this. Got very excited looking at extJS over the Summer, thought “Gee, I know lots about ASP.NET and SQL Server, how do I make this work?”

    I know nothing about MVC, but will now take a look at putting these two technologies.


  4. Thank you. I’m looking to get started with Asp.Net MVC and ExtJs. Any chance I can get a copy of your demo so I can see how to get started?



Leave a Reply

Your email address will not be published. Required fields are marked *