Visual Studio 2008之语言特性 查询语法。学习一门新的语言,总要从其语法学起。为了方便大家学习,同时也为了自己学习,就将ScottGu的这篇关于3.5中增加的新的特性帖子译了出来。这次我翻译采用中汉对照的方式。
What is Query Syntax?
什么是查询语法?
Query syntax is a convenient declarative shorthand for expressing queries using the standard LINQ query operators. It offers a syntax that increases the readability and clarity of expressing queries in code, and can be easy to read and write correctly. Visual Studio provides complete intellisense and compile-time checking support for query syntax.
查询语法是用标准的LINQ查询操作符来声来为表达式声明的一个方便的速记方法。它提供了一种增强了程序的可读性和明了性的语法,并且易读易写,写的过程中不容易出现错误。VS为查询语法提供了完全的智能提示和编译时检查。
Under the covers the C# and VB compilers take query syntax expressions and translate them into explicit method invocation code that utilizes the new Extension Method and Lambda Expression language features in "Orcas".
根据报道,C#和VB编译器识别查询语法表达式并将它们译为强制声明的方法,这些新方法利用了在"Orcas"中的新的"Extension Method"和Lambda Expression。
Query Syntax Example:
查询语法事例:
In my previous language series posts, I demonstrated how you could declare a "Person" class like below:
在我本系统的前几篇帖子中,我说明了如何声明一个像下边的类:

We could then use the below code to instantiate a List
我们可以利用下面的代码来实例化一个具有peopeo的values的List
The query syntax expression above is semantically equivalent to the below code that uses LINQ extension methods and lambda expressions explicitly:
上边的查询语法在语义上和下面显示调用LINQ extension methods和lambda expressions的代码是等同的:

The benefit with using the query syntax approach is that it ends up being a little easier to read and write. This is especially true as the expression gets richer and more descriptive.
用查询语法的好处是它更易读和易写一些,显然是这样,因为表达式变得更具有表达性。
Query Syntax - Understanding the From and Select Clauses:
查询语法:了解From和Select的结构:
Every syntactic query expression in C# begins with a "from" clause and ends with either a "select" or "group" clause. The "from" clause indicates what data you want to query. The "select" clause indicates what data you want returned, and what shape it should be in.
在C#中,每一个查询表达式的语法是以"from"开始,以“select”或“group"结束。"from"关键字表明了你想查询哪些数据,"select"关键字表明了你想返回哪些数据,这些数据以什么类型返回。
For example, let's look again at our query against the List
例如,让我们看一下对于List
In the above code snippet the "from p in people" clause is indicating that I want to perform a LINQ query against the "people" collection, and that I will use the parameter "p" to represent each item in the input sequence I am querying. The fact that we named the parameter "p" is irrelevant - I could just have easily named it "o", "x", "person" or any other name I wanted.
在上边的代码片断中,"from p in peple"声明了我想从people集合中查询出数据,并且声明了将以参数"p"来代码我查询的每一个数据项。事实上我们将该参数命名为"p"跟查询是无任何关联的,我可以将它命名为"o","x","person"或其他任何我想命名的名字都可以。
In the above code snippet the "select p" clause at the end of the statement is indicating that I want to return an IEnumerable sequence of Person objects as the result of the query. This is because the "people" collection contains objects of type "Person", and the p parameter represents Person objects within the input sequence. The datatype result of this query syntax expression is consequently of type IEnumerable
在上边的代码片断中,表达式末尾的“select p"语句声明了我想返回一个IEnumerable Person对象来作为我查询的结果。这是因为"people"集合包括了"Person"类型的对象,并且参数"p"代表了Person对象。因此,该查询表达式的返回的数据结果的类型是IEnumberable