Common Linq / Standard Query Operator mistakes/mis-steps?

后端 未结 4 1058
感动是毒
感动是毒 2021-01-03 09:30

For programmers that do not come from a functional programming background, are there an mistakes to avoid?

4条回答
  •  离开以前
    2021-01-03 10:12

    Understanding the semantics of closures.

    While this isn't a problem limited to just LINQ queries, closed over variables do tend to come up more frequently in LINQ because it's one of the most common places where lambda expressions are used.

    While closures are very useful they can also be confusing and result in subtly incorrect code. The fact that closures are "live" (meaning that changes to variables outside of the captured expression are visible to the expression) is also unexpected to some developers.

    Here's an example of where closures create problems for LINQ queries. Here, the use of closures and deferred execution combine to create incorrect results:

    // set the customer ID and define the first query
    int customerID = 12345;
    var productsForFirstCustomer = from o in Orders
                                   where o.CustomerID = customerID
                                   select o.ProductID;
    
    // change customer ID and compose another query...
    customerID = 56789; // change the customer ID...
    var productsForSecondCustomer = from o in Orders
                                    where o.CustomerID = customerID
                                    select o.ProductID;
    
    if( productsForFirstCustomer.Any( productsForSecondCustomer ) )
    {
       // ... this code will always execute due to the error above ...
    }
    

    This query will always enter the body of the if() { } statement because by changing the customerID value it affects the execution of both queries - they, in fact, both use the same ID since the customerID variable is captured in both LINQ statements.

提交回复
热议问题