Share via


Daniel Moth’s Linq on Channel 9

We are working on putting together a session for MSDN Events on LINQ and how it changes the way that developers work with data in their applications. The basic question of what is LINQ is fairly straightforward, in that it is the name given to a collection of new language features that are part of .NET 3.5. While the next framework (3.5) does add a number of components for LINQ, it also utilizes a number of language features that were added in previous versions including 2.0 and 3.0.

Daniel Moth is a former MVP who has been blogging recently (https://www.danielmoth.com) on LINQ and even posted a video on Channel9 which does a great job of describing how LINQ evolves from existing language features. I summarize some of the points on how he does it with some screen shots, etc., but to get the most from it, click on the link & watch it for yourself.

The synopsis

Before LINQ

With LINQ

How do we get there?

Add Helper.cs

MyEnumerator.cs

And then using these we rewrite in C# 2.0

Omit types that can be inferred by compiler  <Person>, <Person, TempType>

Use inline anonymous methods to replace delegates  MyPredicate & MySelector with code from helper.cs

Now use .Net 3.0 features

Change delegates to Lambda expressions by removing delegate and adding => after type

Drop type because compiler can infer type for p

Get rid of "return" keyword & extra curly braces

Next using object initializers we can create a new type & further simplify the syntax of results2…

Finally we use anonymous type because TempType is not used elsewhere

To have compiler infer type use the keyword var in place of TempType

Finally we look at extension methods to provide visibility to Select & Where functions declared in MyEnumerator.cs

And last we take care of results2? Do some formatting and we have

Which brings us back to