C# 3.0 – LINQ

I get LINQ now! wohoo!
 
All the syntax sugar added to C# 3.0 seems to be there for the benefit of LINQ. Makes it short and readble.
Can’t translate SQL into C#, right? Right? So, LINQ is the next best thing, especialy if lets you query any object/store.
 
And now to the example from C# spec that made me see the light:

The example

from c in customers
group c by c.Country into g
select new { Country = g.Key, CustCount = g.Count() }

is translated into

from g in
      from c in customers
      group c by c.Country
select new { Country = g.Key, CustCount = g.Count() }

the final translation of which is

customers.
GroupBy(c => c.Country).
Select(g => new { Country = g.Key, CustCount = g.Count() })

 
You can see the elemetal building blocks emerging: lambda expressions, extension methods, anonymous types, instance initializers.
 
Expression trees is what allows different backing for the same LINQ syntax:

Because a conversion exists to Func, a conversion also exists to Expression>.

Func f = x => x + 1;                   // Code

Expression> e = x => x + 1;       // Data

Following these assignments, the delegate f references a method that returns x + 1, and the expression tree e references a data structure that describes the expression x + 1.

Next on my list: finding out how are LINQ to XML, – to Objects, etc. work…

Found this nice description: http://community.bartdesmet.net/blogs/bart/archive/2007/04/06/the-iqueryable-tales-linq-to-ldap-part-1-key-concepts.aspx

 

Advertisements
C# 3.0 – LINQ

One thought on “C# 3.0 – LINQ

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s