FindAll Vs Where

This question already has an answer here:

  • LINQ, Where() vs FindAll() 4 answers

  • You can find your answer here: LINQ, Where() vs FindAll() . Basically if you call .ToList() on your "Where" they would be the same.

    You can find more about the differences between deferred and immediate execution: https://code.msdn.microsoft.com/LINQ-Query-Execution-ce0d3b95


    我最好的猜测是在调用Where的时候发生了什么,它创建一个枚举器,并在代码中实际使用结果的地方(例如实际调用该枚举器的MoveNext和(get_)Current,例如从ToList)。


    Yes, Where is a lazy version of findall. FindAll() is a function on the List type, it's not a LINQ extension method like Where. The FindAll method on List, which is an instance method that returns a new List with the same element type. FindAll can only be used on List instances whereas LINQ extension methods work on any type that implements IEnumerable.

    The main difference (besides what they're implemented on: IEnumerable vs. List) is that Where implements deferred execution, where it doesn't actually do the lookup until you need it, (using it in a foreach loop for example). FindAll is an immediate execution method.

    I will refer to a data structure called an expression tree to understand deferred execution, you need only grasp that an expression tree is a data structure like a list or queue. It holds a LINQ to SQL query not the results of the query, but the actual elements of the query itself.

    To understand the Where Working we need to see that if we write a code

    var query = from customer in db.Customers  
            where customer.City == "Paris"
            select customer;              
    

    Query does not execute here whereas it execute in the foreach loop

    TO understand LINQ and Deferred Execution

    链接地址: http://www.djcxy.com/p/88212.html

    上一篇: 如何从手机中的图库中选择视频?

    下一篇: FindAll Vs在哪里