2

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?

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+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).
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. :)
1

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.

Comments

1

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.

Comments

0

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.

Comments

0

Since I don't see any example of usage of Dynamic LINQ here, here it goes:

In my faculty project I had a situation where I would use Repository pattern to make an abstraction over my used database technology, particulary Entity Framework. In my Repository I would have a method something like this:

public IEnumerable<T> Find(Expression<Func<T, bool>> predicate);

As you can see, an Expression is used as the predicate.

Also, I had client-server communication over WCF. Since Expressions are not Serializable, I had to use Dynamic LINQ where I would just send string representation of predicates and use them with my Repository.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.