LINQ to Entities - OrderBy()。ToLisT()vs. ToList()。OrderBy() [英] LINQ to Entities -- OrderBy().ToLisT() vs. ToList().OrderBy()

查看:155
本文介绍了LINQ to Entities - OrderBy()。ToLisT()vs. ToList()。OrderBy()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var context = new SomeCustomDbContext()

// LINQ to Entities?
var items = context.CustomItems.OrderBy(i => i.Property).ToList();

// LINQ to Objects?
var items2 = context.CustomItems.ToList()。OrderBy(i => i.Property);

(Q1)我认为第一种方法是 LINQ to Entities 其中EF构建一个更具体的SQL语句来传递,将排序工作放在数据库上?



(Q2)第二种方法 LINQ to Objects 其中LINQ拖动整个在订购之前收集到内存中( ToList()枚举?),从而在服务器端(在这种情况下是Web服务器)中留下负担?



如果是这种情况,我可以快速看到L2E有利的情况(例如,在将它们拉入内存之前过滤/修剪集合)。



(Q3)但是,我应该注意哪些其他细节/权衡,或者方法2可能是有利于第一种方法?



更新:



使用EntityFramework,只要底层存储库/数据源实现 IQueryable< T> ,这仍然是真的?而且如果这两个语句都不会导致内存中的 LINQ to Objects 操作

解决方案<
  • 是的

  • 是的

  • / ol>

    你是正确的,调用 ToList()强制linq到实体来评估和返回结果作为列表。如你所怀疑的,这可能会带来巨大的性能影响。



    有些情况下,linq到实体不能弄清楚如何解析看起来像一个完美简单的查询 Where(x => SomeFunction(x)))。在这些情况下,您通常别无选择,只能调用 ToList()并对内存中的集合进行操作。






    为了回应您的更新:



    ToList()一切都在它之前立即进行评估,而不是延期执行。举个例子:

      someEnumerable.Take(10).ToList(); 

    vs

     <$ 。C $ C> someEnumerable.ToList()取(10); 

    在第二个例子中,任何关于 someEnumerable 必须先执行前10个元素。如果 someEnumerable 正在做一些劳动密集型的工作(例如使用 Directory.EnumerateFiles()从磁盘读取文件),这可能具有非常真实的性能影响。


    I'm looking for confirmation/clarification with these LINQ expressions:

    var context = new SomeCustomDbContext()
    
    // LINQ to Entities?
    var items  = context.CustomItems.OrderBy(i => i.Property).ToList();
    
    // LINQ to Objects?
    var items2 = context.CustomItems.ToList().OrderBy(i => i.Property);
    

    (Q1) Am I correct in thinking the first method is LINQ to Entities where EF builds a more specific SQL statement to pass on, putting the ordering effort on on the database?

    (Q2) Is the second method LINQ to Objects where LINQ drags the whole collection into memory (the ToList() enumeration?) before ordering thus leaving the burden on the server side (the web server in this case)?

    If this is the case, I can quickly see situations where L2E would be advantageous (ex. filtering/trimming collections before pulling them into memory).

    (Q3) But are there any other details/trade-offs I should be aware of, or times when "method 2" might be advantageous over the first method?

    UPDATE:

    Let's say we are not using EntityFramework, this is still true so long as the underlying repository/data source implements IQueryable<T> right? And if it doesn't both these statements result in LINQ to Objects operations in memory?

    解决方案

    1. Yes.
    2. Yes.
    3. Yes.

    You are correct that calling ToList() forces linq-to-entities to evaluate and return the results as a list. As you suspect, this can have huge performance implications.

    There are cases where linq-to-entities cannot figure out how to parse what looks like a perfectly simple query (like Where(x => SomeFunction(x))). In these cases you often have no choice but to call ToList() and operate on the collection in memory.


    In response to your update:

    ToList() always forces everything ahead of it to evaluate immediately, as opposed to deferred execution. Take this example:

    someEnumerable.Take(10).ToList();
    

    vs

    someEnumerable.ToList().Take(10);
    

    In the second example, any deferred work on someEnumerable must be executed before taking the first 10 elements. If someEnumerable is doing something labor intensive (like reading files from the disk using Directory.EnumerateFiles()), this could have very real performance implications.

    这篇关于LINQ to Entities - OrderBy()。ToLisT()vs. ToList()。OrderBy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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