Sunday, February 11, 2007

Wes Dyer talking about LINQ

More browsing today on MSDN lead me to an interview with Wes Dyer taking about LINQ. He's one of the guys on the C# compiler team. A very bright chap indeed and he gives one of the best explanations of how LINQ works that I've yet seen. The white board session especially in the second half of the video is a fantastic mind expanding ride and shows how LINQ is far far more than just queries. Typically I think Microsoft's marketing have probably muddied the waters again by calling it 'language integrated query' because, although querying is something you can do with it, it's much more like 'functional programming in C#'. For example, Wes showed how you can replace typical looping patterns with LINQ 'queries'. Take this little code snippet to get the even numbers from an array of numbers:
int[] list = int[] {1,2,3,4,5,6,7};
List<int> evenNumbers = new List<int>();
foreach(int n in list)
if(n % 2 == 0)
You can write the same thing with LINQ:
int[] list = int[] {1,2,3,4,5,6,7};
var evenNumbers =
from n in list
where n % 2 == 0
select n;
I think it's much easier to read the intention of the LINQ version. One thing that really struck me was how they use the custom iterators that I talked about in my last post to create the 'where' and 'select' clauses in LINQ. A very bright light bulb went off in my head at that point! Wes Dyer's blog is also well worth checking out.

No comments: