Tag Archives: asp.net mvc

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.