为什么LINQ到对象方法的顺序计数 [英] Why the order of LINQ to objects methods counts

查看:86
本文介绍了为什么LINQ到对象方法的顺序计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了这个问题的答案,解释的顺序LINQ到对象中的方法有差别。我的问题是,为什么?

如果我写的LINQ to SQL查询,没关系的LINQ方法的顺序 - 预测例如:

  session.Query<人>()排序依据(X => x.Id)
                       。凡(X => x.Name ==gdoron)
                       .ToList();
 

这位前pression树将被转化为理性的SQL是这样的:

  SELECT *
  向个人
  其中name ='gdoron
  订单来自序号;
 

当我运行查询,SQL查询,将根据前pression树不管怎么建怪异的方法的顺序。
为什么它不与 LINQ的工作原理相同的对象
当我列举一个的的IQueryable 的所有的投影可以放置在一个合理的顺序(如排序后,凡)就像数据库优化器。

解决方案
  

为什么不使用LINQ以这种方式工作的对象?

的LINQ to Objects不使用EX pression树。该语句被直接转变成一系列方法调用,其中每个作为一个普通的C#方法运行

因此​​,到对象在LINQ如下:

  VAR的结果= collection.OrderBy(X => x.Id)
                   。凡(X => x.Name ==gdoron)
                   .ToList();
 

获取变成直接的方法调用:

  VAR的结果= Enumerable.ToList(
                   Enumerable.Where(
                     Enumerable.OrderBy(集合,X => x.Id)
                     X => x.Name =gdoron
                   )
                 );
 

通过查看方法调用,你可以看到为什么订货事宜。在这种情况下,通过将排序依据第一,你有效嵌套成最内的方法调用。这意味着整个集合将得到有序当resutls列举。如果你要切换的顺序:

  VAR的结果=集合
                   。凡(X => x.Name ==gdoron)
                   .OrderBy(X => x.Id)
                   .ToList();
 

那么产生方法链开关:

  VAR的结果= Enumerable.ToList(
                   Enumerable.OrderBy(
                     Enumerable.Where(集合,X => x.Name =gdoron),
                     X => x.Id
                   )
                 );
 

这,反过来,意味着只有过滤的结果将需要被分类为的OrderBy执行

I read this question's answers that explain the order of the LINQ to objects methods makes a difference. My question is why?

If I write a LINQ to SQL query, it doesn't matter the order of the LINQ methods-projections for example:

session.Query<Person>().OrderBy(x => x.Id)
                       .Where(x => x.Name == "gdoron")
                       .ToList();

The expression tree will be transformed to a rational SQL like this:

  SELECT   * 
  FROM     Persons
  WHERE    Name = 'gdoron'
  ORDER BY Id; 

When I Run the query, SQL query will built according to the expression tree no matter how weird the order of the methods.
Why it doesn't work the same with LINQ to objects?
when I enumerate an IQueryable all the projections can be placed in a rational order(e.g. Order By after Where) just like the Data Base optimizer does.

解决方案

Why it doesn't work this way with LINQ to objects?

LINQ to Objects doesn't use expression trees. The statement is directly turned into a series of method calls, each of which runs as a normal C# method.

As such, the following in LINQ to Objects:

   var results = collection.OrderBy(x => x.Id)
                   .Where(x => x.Name == "gdoron")
                   .ToList();

Gets turned into direct method calls:

   var results = Enumerable.ToList(
                   Enumerable.Where(
                     Enumerable.OrderBy(collection, x => x.Id),
                     x => x.Name = "gdoron"
                   )
                 );

By looking at the method calls, you can see why ordering matters. In this case, by placing OrderBy first, you're effectively nesting it into the inner-most method call. This means the entire collection will get ordered when the resutls are enumerated. If you were to switch the order:

   var results = collection
                   .Where(x => x.Name == "gdoron")
                   .OrderBy(x => x.Id)
                   .ToList();

Then the resulting method chain switches to:

   var results = Enumerable.ToList(
                   Enumerable.OrderBy(
                     Enumerable.Where(collection, x => x.Name = "gdoron"),
                     x => x.Id
                   )
                 );

This, in turn, means that only the filtered results will need to be sorted as OrderBy executes.

这篇关于为什么LINQ到对象方法的顺序计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆