开发者

Difference between System.Linq.Dynamic and System.Linq?

Using ReSharper, I occasionally get quick-fix suggestions for importing a namespace for a LINQ operation. So given the following code in a bra开发者_StackOverflow中文版nd-new class:

linqToSqlDataContext.Customers.Count();

I get a quick-fix drop down as follows:

Difference between System.Linq.Dynamic and System.Linq?

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


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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜