Yesky首页| 产品报价| 行情| 手机 | 数码 | 笔记本 | 台式机 | DIY硬件 | 外设 | 网络 | 数字家庭 | 评测 | 软件 | e时代 | 游戏 | 图片 | 壁纸 | 群乐 | 社区 | 博客 | 下载
软件频道>程序开发>JavaVBVCDelphiC/C++Web开发微软专栏移动数据库程序人生软件工程|产品中心下载什么是软件架构
您现在的位置: 天极网 > 开发频道 > Visual Studio 2008之语言特性 查询语法
全文
群乐:.NET

Visual Studio 2008之语言特性 查询语法

2007-11-14 10:02 作者: 韩现龙的博客 出处: 天极网软件频道 责任编辑:幽灵

  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. In my samples above you'll notice that all of the query syntax assignments are to IEnumerable, IEnumerable, IEnumerable, IEnumerable, and IEnumerable variables.

  默认的,查询表达式的结果是一个类型为IEnumerable的变体类型,在我以上的例子中你注意到所有的表达式声明语句是IEnumerable, IEnumerable, IEnumerable, IEnumerable, and IEnumerable variables.

  One of the nice characteristics of IEnumerable interfaces is that objects that implement them can defer the actual execution of the queries until a developer first attempts to iterate over the values (this is accomplished using the "yield" construct that was first introduced with C# 2.0 in VS 2005). LINQ and query syntax expressions take advantage of this feature, and defer the actual execution of queries until the first time you loop over the results. If you never iterate over the IEnumerable result, then the query is never executed.

  IEnumeralb接口的其中一个特点是实现它们的对象可以延迟查询的执行,直到开发者首次使用该声明对象(这是在VS2005中用C#2.0介绍的"yield"结构)。LINQ 和查询语法表达式利用了该特性,并且延迟了查询,直到你首次遍历结果集。如果你一直不用IEnumerale结果集,它就一直不会执行。

  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 or an array containing the results.

  如果你不想延迟执行查询,想立即执行它们,你可以用内置的ToList()和ToArray()方法来返回一个包含结果集的或者List或者是array的类型。

  For example, to return a generic-based List collection:

  例如,返回一个基于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

  点击查看原文>>

共5页。 9 1 2 3 4 5
网友关注
最新上市
编辑推荐
欢迎订阅天极网RSS聚合资讯:http://www.yesky.com/index.xml