c# - LINQ 上下文中的术语 "deferred query evaluation"指的是什么?

标签 c# linq

LINQ 上下文中的术语“延迟查询评估”指的是什么? (请举例说明)。

最佳答案

对于 LINQ,在实际枚举结果之前不会评估您构建的查询。在构建查询时,您所做的只是构建一个表达式树、一系列委托(delegate),或者使用其他语言结构(例如 yield 语句)来推迟执行。在需要查询结果之前,表达式树(委托(delegate)/ yield )不会产生实际查询。这允许您构建一个查询,直到最后一刻才对其进行评估,从而使结果集尽可能小。例如,在 LINQ to SQL 中我们有:

// no query is executed by this statement
var query = from product in db.Products
            select product;


// a select count(*) from Products is executed by this statement
var productCount = query.Count();

// a select ... from Products where ID == 3 is executed by this statement
var products = query.First( p => p.ID == 3);


// a select .. from Products is executed by this statement
foreach (var product in query)
{
   ...
}

可以在 http://language-integrated-query.com/Linq_Deffered_Query.aspx 找到对此的一个很好的讨论。和 http://msdn.microsoft.com/en-us/library/bb308959.aspx .

关于c# - LINQ 上下文中的术语 "deferred query evaluation"指的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1821394/

相关文章:

c# - 为什么 Mono 上的 NetMQ DealerSocket 不向 Debian Wheezy 上的服务器发送消息,但在 Windows 上却发送消息?

c# - 带有 ServiceBus 队列的 ODATA 过滤器

单个对象的 C# 数据转换

sql - 如何使用 LINQ to SQL 处理 IN 子查询?

c# - 什么是 HtmlTokenizer?

c# - 检索 json WebResponse c# 中的特定行

C#:方法签名?

c# - 如何在 linq 中有效地连接字符串?

c# - 无法执行 not-in

c# - GroupBy 项目不相等