Tag Archives: C#

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.

Yummy…. Crow!

Yes, I will accept I need to eat some crow. I’ve been playing a lot recently with the .NET framework and C#. My PHP mistress got kinda boring. Same-o, same-ol’. I decided to give .NET another whirl because I wanted to port the ExtJS presentation I did for the local Spring users group to .NET. 

Since I hadn’t really gotten in to .NET since v2.0, I got up to date with Visual Studio 2008 and .NET 3.5sp1. I knew I neeeded JSON for my AJAX, so I thought about starting with WCF. But to fully dig in to WCF, I needed to understand the new features in .NET 3.5, so I found some good tutorials on what was new.

I was really blown away with how far .NET has progressed. I’m a big fan of JavaScript, and C# in .NET 3.5 actually starts to look a lot like it. You can declare variables with the var keyword and you have anonymous classes and lambdas. I actually played with LINQ, and I have say I like it a lot. I will humbly eat that crow pie over the trash talking I gave LINQ serveral posts ago.

I’m still not down with the postback model for ASP.NET, but am playing with ASP.NET MVC, which is getting pretty mature in its fifth preview release. It will definitely be the way I head. I just need to sort out the WCF stuff for the JSON/AJAX calls and I’ll be a happy camper.

So in an epic “I will not eat green eggs and ham” kind of way, I’ve decided I really like those green eggs and ham. But it is a lot more like green eggs and crow in this case.