tags:

views:

73

answers:

4

Using ReSharper, I occasionally get quick-fix suggestions for importing a namespace for a LINQ operation. So given the following code in a brand-new class:

linqToSqlDataContext.Customers.Count();

I get a quick-fix drop down as follows:

alt text

Which should I choose, and what is the difference between them?

+1  A: 

Dynamic LINQ is a non-typesafe version of LINQ. That takes strings rather than lambdas to generate the queries.

Unless you need any of the specialist functionality that this will give you use the Enumerable version instead.

Martin Harris
+1  A: 

Scott Hanselman did a good explanation of DynamicQueryable. Basically it allows you to have a little more dynamism where the parameters may change during runtime.

Mike Killey
+2  A: 

System.Linq.Dynamic is the namespace for Dynamic LINQ. You shouldn't be seeing that as an option unless you've added a reference to the Dynamic LINQ assembly though. Have you done so? You should only do that if you actually want to use Dynamic LINQ.

Dynamic LINQ lets you express queries as text - a bit like with DataTable.Select. I've personally never found a use for it, but you may want it. It should be a deliberate choice though. Most of the time you'll be fine with the statically typed LINQ to Objects.

EDIT: As per the OP's comment, the code for Dynamic LINQ could have been added directly to the project, rather than referenced as a separate assembly. Even if you do actually want to use Dynamic LINQ, I'd strongly recommend keeping it in a separate assembly rather than mixing it in with your own code.

Jon Skeet
+1 and I'll accept this answer if you could update with the notion that (as per my own answer below) someone could have added the actual DynamicLinq extensions class directly to the project (i.e. not a reference, but actual code in the project).
Neil Barnwell
@Neil: Eewww :) But answer duly edited...
Jon Skeet
@jonskeet Oh c'mon, seriously? I saw your "Making C# do horrible things that might bring about the end of the world" talk at NxtGenUG's "Fest 10" in Bournemouth... :P But yeah, "eewww" indeed. :)
Neil Barnwell
A: 

Argh! The answer in the end was that one of my colleagues added the DynamicQueryable extensions class to our project (from http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx), and ReSharper was picking that up.

Neil Barnwell