Blog

Hello LINQ in .NET 2.0

December 31, 2008

When using Visual Studio 2008, it is possible to use most of the new language additions of C# 3.0 in a .NET 2.0 project. This because the C# 3.0 compiler is used for both .NET 2.0, .NET 3.0 as well as for .NET 3.5 projects in Visual Studio. This means that local variable inference, object initializers, extension methods, lambda expressions, etc. … can be used in a .NET 2.0 project.

The one thing that is missing however is LINQ because those extensions are packaged in the new System.Core assembly which comes only with .NET 3.5.

However, last week I accidentally stumbled into LINQBridge which enables you to write LINQ to Objects queries targeting .NET 2.0. The only requirements for this is that you need to have Visual Studio 2008 and a reference to the LINQBridge assembly. Note that the current implementation does not support other LINQ providers besides LINQ to Objects (no LINQ to XML and certainly not LINQ to SQL).

The following code is written for targeting the .NET 2.0 runtime.

public class Actor
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public Int32 ShoeSize { get; set; }
}
 
public class Program
{
    static void Main()
    {
        var actors = new List<Actor>()
        {
            new Actor() { FirstName = "Chuck", 
                          LastName = "Norris", 
                          ShoeSize = 46 },
            new Actor() { FirstName = "Adam", 
                          LastName = "Sandler", 
                          ShoeSize = 41 },
            new Actor() { FirstName = "Steven", 
                          LastName = "Seagal", 
                          ShoeSize = 48 }              
        };
 
        var actorsWithBigFeet = from actor in actors
        where actor.ShoeSize > 45
        select actor;
 
        foreach(Actor bigfoot in actorsWithBigFeet)
        {
            Console.WriteLine("{0} {1} has shoe size {2}.", 
                              bigfoot.FirstName, 
                              bigfoot.LastName, 
                              bigfoot.ShoeSize);
        }
 
        Console.Read();
   }
}

Just to let you know that if you are stuck with .NET 2.0 like me, then life shouldn’t be that bad either ;-). Kudos to project owners for making this possible for us poor developers!

Profile picture of Jan Van Ryswyck

Jan Van Ryswyck

Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Comments

About

Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be