LINQ (Language Integrated Query)

LINQ, as you may already know, looks like TSQL queries, except they are twisted upside down, and written in right inside of C#. You have probably already heard about the benefits, so I won’t go into those for now. In here, we are going to talk about Query Expression Translation. You can write queries in LINQ using various keywords such as “from”, “where”, “select” etc.

Those queries, get translated into plain vanilla C# 3.0 code, and only then type-binding, overload resolution etc. happens. Once the Query has been translated to C#, it is then executed as regular method invocations – where of course you have various C# protections such as a method being missing, data types mismatched so on and so forth.

So, LINQ –>  Query –> TranslatedToC#3.0 –> Method Invocation.

So LINQ = Method Invocation? YES – THAT IS WHAT MAKES LINQ, Nothing but Plain vanilla C# 3.0. All those new language constructs, along with Query Expression Translation, make LINQ possible.

Lets understand with the help of an example.

In my last post Demystifying C# 3.0 – Part 5: Object and Collection Initializers, I had a simple example demonstrating Object & Collection Initializers. Basically, we got a List<Monkey> back as shown below –

var theWhiteHouseStaff =
new List<Monkey>
{
new Monkey{ Name = “George W Bush”, Age = 16},
new Monkey{ Name = “Donald Rumsfield”, Age = 16},
new Monkey{ Name = “Condolezza Rice”, Age = 16},
new Monkey{ Name = “Dick Cheney”, Age = 16}

} ;

It is notable that List<T> implements IEnumerable<T>, so the above is a queryable object. So, you could write a query, that looks like –

var q =
from staff in theWhiteHouseStaff

select new {staff.Name} ;

Practical Application: “var q” <– Anonymous Type (+). An anonymous type that holds an Anonymous Type with one property “staff.Name”. Also, “var” lets you create an Implicitly typed local variable (+) . This means,
a) You didn’t have to declare/write a class structure to hold a type with one property called “Name” of data type string.
b) You don’t have to maintain that either – you can change the structure of the above query, and “it just works” 🙂

Now, the above query, can also be written like this –

var q =
from staff in theWhiteHouseStaff

select new { Name = staff.Name } ;

Practical Application: “new { Name = staff.Name }” <– Object Initializer (+). The anonymous type, is being initialized by an Object Initializer. The anonymous type doesn’t have logic, or a constructor. But it does have public setters on it’s properties, so there you go – the Object Initializer can now take advantage of those, and the query is slowly decomposing into plain vanilla C# 3.0.

The above query, further decomposes into the below –

var q = theWhiteHouseStaff.Select(staff => new {staff.Name}) ;

WHOAA !!!, lets look at this query once again, only color coded this time 😉

var q = theWhiteHouseStaff.Select(staff => new {staff.Name}) ;

(PS: If the RSS Feed eats the color coding, I suggest you come see it on my blog)

Practical Application:
The yellow var q, is an Anonymous Type (+) “q”, the “var” lets you create an implicitly typed local variable (+).
The theWhiteHouseStaff is an IEnumerable<T>
The green Select is an Extension Method  (+).
The Gray staff => new { staff.Name } is a Lambda expression (+) that is participating in type-inference (+).
and the new {staff.Name} is an Object Initializer (+).

So, the LINQ Query

var q =
from staff in theWhiteHouseStaff

select new {staff.Name} ;

is *absolutely* the same as the C# 3.0 language construct –

var q = theWhiteHouseStaff.Select(staff => new {staff.Name}) ;

Thus, the (Linq) query expression has been translated (to plain vanilla C# 3.0). This is called as Query Expression Translation, and this is the reason behind C# 3.0 enhancements.

Comments are closed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: