Tuesday, May 09, 2006

Closing the circle... Finding that missing Linq

I've been looking at Linq recently in the context of using it in anger to provide in-memory filtering of List<thing> instances.

My problem was that I had an existing class library project that I needed to "Linq enable", and just adding the references to System.Query et al just doesn't work.

It turns out that you need to modify the project file itself.

You can do this in any text editor easily enough, but I found this trick for doing so in VS2005.

  1. Right click on the project in the solution and select "Unload Project"
  2. Then right-click again and select "Edit project file" from the context menu to open the project file up in the editor. This gives you all the benefits of syntax highlighting, intellisense, etc. Good stuff.
  3. Make your changes to the .csproj file and save 'em
  4. Right click on the project in the Solution Explorer and select "Reload project"

Now, to Linq enable a project, you need to find the line in the project file that reads

<import project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

and replace it with the following lines

<import project="$(ProgramFiles)\LINQ Preview\Misc\Linq.targets" />
<!-- Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" / -->

Of course, your path to the Linq.targets file may be different if you've installed Linq anywhere other than the default.

Recompile your project and you'll find that all the Linq goodness is available and working within your existing project.

No comments: