Query Syntax - Understanding Deferred Execution, and using ToList() and ToArray()
查询语法--了解Deferred Execution,用ToList()和ToArray()方法
By default the result of a query syntax expression is a variable of type IEnumerable
默认的,查询表达式的结果是一个类型为IEnumerable
One of the nice characteristics of IEnumerable
IEnumeralb
For example, consider the below LINQ to SQL example:
例如,看如下的LINQ to SQL事例:

The database will be hit and the values to populate our Category objects will be retrieved not when the query syntax expression is declared - but rather when we first try and loop over the results (indicated above with the red arrow).
不是在查询语法声明时将数据库查询和将值加载到我们的Category对象中,而是当我们首次试图循环结果集时(上边用红色箭头表明的部分)
This deferred execution behavior ends up being really useful because it enables some powerful composition scenarios where we can "chain" multiple LINQ queries and expressions together. For example, we could feed the result of one expression into another - and by deferring the execution allow an ORM like LINQ to SQL to optimize the raw SQL based on the entire expression tree. I'll show examples of how to use this in a later blog post.
这种延迟执行的行为的非常有用,因为它使得一些在我们能“链接”LINQ 查询和表达式到一起的强大的组合情景成为可能。例如,我们可以将一个表达式的填充到另一个中--并且通过延迟执行,允许像LINQ to SQL的ORM根据整个表达树来优化原始的SQL语句。在下几篇博客中,我将展示一些如何使用延迟执行的例子。
How to evaluate the query syntax expression immediately
如何快速评估查询表达式
If you don't want to defer the execution of queries, and instead want to execute them immediately, you can use the built-in ToList() and ToArray() operators to return either a List
如果你不想延迟执行查询,想立即执行它们,你可以用内置的ToList()和ToArray()方法来返回一个包含结果集的或者List
For example, to return a generic-based List
例如,返回一个基于List

and to return an array:
返回一个数组:

In both cases above the database will be hit and the Category objects populated immediately.
在上边这两个例子中,将会立即查询数据库,并且将查询结果填充到Category对象中。
Summary
总结
Query syntax provides a very convenient declarative shorthand for expressing queries using the standard LINQ query operators. It offers a syntax that is very readable, and which works against any type of data (any in-memory collection, array, XML content, or against remote data providers like databases, web-services, etc). Once you become familiar with the syntax, you can immediately apply the knowledge everywhere.
查询语法是用标准的LINQ查询操作符来声来为表达式声明的一个方便的速记方法。它提供了一种增强了程序的可读性和明了性的语法,并且易读易写,写的过程中不容易出现错误。VS为查询语法提供了完全的智能提示和编译时检查。
In the not too distant future I'll finish the last segment of this language series - which will cover the new "anonymous types" feature. I'll then move on to cover some super practical examples of using all of these language features in the real world (especially using LINQ against databases and XML files).
很快我将结束该语言的最后一个部分--该部分涵盖了新的“anonymous types"特性。届时我将用这些语言在现实世界中做一些高级实用的例子(尤其是用LINQ来对数据库和XML文件来操作)
Hope this helps,
Scott