Common Linq / Standard Query Operator mistakes/mis

对于不是来自功能编程背景的程序员,是否有错误需要避免?


The biggest mistake people tend to make is to misunderstand the laziness and evaluation rules for a LINQ query:

Queries are lazy: they are not executed until you iterate over them:

// This does nothing! No query executed!
var matches = results.Where(i => i.Foo == 42);

// Iterating them will actually do the query.
foreach (var match in matches) { ... }

Also, results are not cached. They are computed each time you iterate over them:

var matches = results.Where(i => i.ExpensiveOperation() == true);

// This will perform ExpensiveOperation on each element.
foreach (var match in matches) { ... }

// This will perform ExpensiveOperation on each element again!
foreach (var match in matches) { ... }

Bottom line: know when your queries get executed.


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

Good question. As Judah points out, the biggest one is that a query expression constructs a query, it does not execute the query that it constructs.

An immediate consequence of this fact is executing the same query twice can return different results.

An immediate consequence of that fact is executing a query the second time does not re-use the results of the previous execution, because the new results might be different.

Another important fact is queries are best at asking questions, not changing state. Try to avoid any query that directly or indirectly causes something to change its value. For example, a lot of people try to do something like:

int number; 
from s in strings let b = Int32.TryParse(s, out number) blah blah blah

That is just asking for a world of pain because TryParse mutates the value of a variable that is outside the query.

In that specific case you'd be better to do

int? MyParse(string s) 
{ 
    int result;
    return Int32.TryParse(s, out result) ? (int?)result : (int?)null;
}
...
from s in strings let number = MyParse(s) where number != null blah blah blah...

IMO when you face LINQ, you must know these topics (they're big sources of errors):

Deferred Execution (on SO)

Closure (on SO - 1)

Closure (on SO - 2)

Closure (Eric Lippert's Blog)

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

上一篇: Singleton模式的替代方法?

下一篇: 常见Linq /标准查询操作员错误/错误