For programmers that do not come from a functional programming background, are there an mistakes to avoid?
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.