Linq Dynamic Extensions

My first experience with functional programming was a Lisp class I took around ’92 or ’93. I thought all those parenthesis were irritating, but more than that, the slipperiness of the whole functional paradigm was – well, frustrating partly describes it, but gratuitous is another way I’d describe it. Who needed it?

Then began a happy interlude of 15 years of procedural/imperative programming – Ada, VB, C, C++, Java, C#, SQL, etc. Ahhhhh.

Now, functional programming is all the rage. You’ve got Scheme, Erlang, Haskell, etc. And when Linq came out, I’ve been seeing all these things like Dynamic Lambda Expressions, parse trees, Func<> delegates, etc. It’s like I’ve been tossed off the dock at age 6 all over again. 🙂

But having spent some time getting acquainted today, it’s not all that problematic. It’s a shift in perspective. And it definitely helps to have specific problems to solve. Case in point: trying to apply Linq Dynamic Extensions to be able to take a strongly-typed predicate like:

OrderBy(c => c.Name);

and turning it into something more dynamic, like:

OrderBy("name");

…the better to be able to generate Linq results on the fly without having to paint yourself in a switch() corner. There’s a set of Dynamic Extensions available in this zip file that allows you to do just that sort of thing. Unusually for Microsoft, they don’t give you a step-by-step guide to actually implement it. The steps turn out to be pretty easy:

Copy the Dynamic.cs file to your project (I put it next to my .dbml file in my library project).

Add this statement to the class file in which you want to use the Dynamic Query functionality:

import System.Linq.Dynamic;

Then you can go ahead and so something like:

var p = (from s in Subject select s).OrderBy("name");

I’m pretty sure that when I get a free minute I’ll really enjoy getting into the functional programming theory in a more leisurely manner. Right now I have projects to get out the door, so I must be more workmanlike by necessity.

Advertisements

0 Responses to “Linq Dynamic Extensions”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: