So you have an application you’ve written in Visual Studio 2005 and you want to dig in, convert this baby, and start using LINQ in Visual Studio 2008. In this post I’m going to outline what steps are involved to get LINQ working based on what providers you may want to use.
In Visual Studio 2008 there’s a new multi-targeting feature that allows you to work in Visual Studio 2008 but be able to write applications targeting either the .NET 2.0, 3.0 or 3.5 Frameworks. ScottGu has a good post that talks about this and so does Rick Strahl. This means that going forward you won’t have to load multiple versions of the Visual Studio IDE onto your machine in order to work on applications targeting previous versions of the framework. (Note: If you are targeting .NET 1.0 or 1.1 frameworks then you’ll still have to load 2002 or 2003 IDEs.) This is good news because it not only saves disk space and context switching, it allows you to take advantage of the new IDE tools like debuggers and editors without the risk of upgrading your current projects. However when you do want to upgrade a project to 3.5 to start taking advantage of LINQ, you need to import some of the new namespaces yourself.
When you first open up a project written in Visual Studio 2005 inside Visual Studio 2008 it will prompt you to upgrade the project. What this actually does is update the project (.vbproj) and solution .(sln) files to be compatible with VS 2008. The project file is actually still backwards compatible with VS 2005 so you can still open it there, the solution file however, is just one way. So if you have a team of developers working with a mix of both VS 2005 and VS 2008 IDEs you’ll need to keep two solution files, but luckily your project files (which change much more often than solution configurations) can be shared.
Now all this upgrade process does is upgrade your project and solution files, your application will still be targeting the .NET 2.0 Framework. In order to upgrade your apps to use new features like LINQ you’ll need to change the target framework and add some references. You’ll also want to turn on the new Option Infer feature. This allows the compiler to infer local variable type declarations by evaluating the right-hand side of the expression. This becomes extremely useful when writing LINQ queries. To enable this, double-click on My Project in the Solution Explorer to open the project properties and select the Compile tab. Under Option Infer select “On”.
Now to change the target framework, click the “Advanced Compile Options…” button and you can change the target in the dropdown:
Select the 3.5 Framework, click OK, and the project will be closed and reopened. If you open the project properties again and look on the References tab you’ll notice that the System.Core.dll version 3.5 is now being referenced automatically for you. However, in order to start using LINQ you’ll need to import a couple namespaces and add a couple more references depending on which LINQ providers you want to use. To enable pure LINQ to Objects, on the References tab under Imported Namespaces select System.Linq. You’ll now be able to write queries that work over objects like:
Dim currentFiles = From File In My.Computer.FileSystem.GetFiles(CurDir) _
In order to write queries that work over DataSets you’ll need to add a reference to System.Data.DataSetExtensions then you’ll need to re-run the Dataset generator on the DataSets you want to write LINQ queries over. Just right-click on the DataSet and select “Run Custom Tool”. This will regenerate the DataSet code so that the DataTables now inherit from a LINQ-ready class called TypedTableBase which is in the System.Data.DataSetExtension namespace. You’ll now be able to write queries that work over typed DataSets. For instance, you can now write:
Dim total = Aggregate Products In Me.CategoryProductDataSet.Products _
Where Products.CategoryID = 1 AndAlso _
Products.Discontinued = False _
Into Sum(Products.UnitPrice * Products.UnitsInStock)
If you want to start using the LINQ to XML provider you’ll need to add a reference to System.Xml.Linq.dll and import the namespace System.Xml.Linq. You’ll now be able to write queries that work over XML like:
Dim survey = XElement.Load(CurDir() & “\questions.xml”)
Dim questions = From q In survey…<question> Select q
Finally, if you want to start using LINQ to SQL classes in your newly upgraded project, it’s really easy. Just right-click and select Add New Item and select the LINQ to SQL classes template which opens up the new O/R designer and automatically adds the reference to System.Data.Linq.dll for you. This allows you to write queries like this (against SQL-server):
Dim countryList = From Customer In Db.Customers _
Where Customer.Country <> “” _
Order By Customer.Country _
Select Customer.Country Distinct
So I hope that clears up how to get started with LINQ in your current applications that you’re bringing into VS 2008. So what are you waiting for? ;-) Happy LINQing.