Jud*_*ngo 11
人们倾向于犯的最大错误是误解了LINQ查询的懒惰和评估规则:
查询是惰性的:在迭代它们之前它们不会被执行:
// 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) { ... }
Run Code Online (Sandbox Code Playgroud)
此外,结果不会缓存.每次迭代它们时都会计算它们:
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) { ... }
Run Code Online (Sandbox Code Playgroud)
底线:知道您的查询何时执行.
| 归档时间: |
|
| 查看次数: |
1110 次 |
| 最近记录: |