Visual Studio 2008之语言特性 查询语法.doc_第1页
Visual Studio 2008之语言特性 查询语法.doc_第2页
Visual Studio 2008之语言特性 查询语法.doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Visual Studio 2008之语言特性 查询语法2007-11-14 10:02作者:韩现龙的博客出处:天极网软件频道责任编辑:幽灵If instead of returning Person objects, I wanted to return just the firstnames of the people in the collection, I could re-write my query like so:如果我不返回一个Person对象,只想返回collectioin中所有人的名字的集合,可以按照如下方式重写该查询语句:Note above how I am no longer saying select p, but am instead saying select p.FirstName. This indicates that I dont want to return back a sequence of Person objects - but rather I want to return a sequence of strings - populated from each Person objects FirstName property (which is a string). The datatype result of this query syntax expression is consequently of type IEnumerable.注意上边我没再用“select p,而是用select p.FirstName。这表明我不想让查询返回Person对象,而是想返回一个字符串-这个字符串是Person这个对象的FirstName属性(是个字符串)中取得的。因此,该查询表达式返回值的数据类型是IEnumerable。Sample Query Syntax Against a Database关于对数据库查询的事例:The beauty of LINQ is that I can use the exact same query syntax against any type of data. For example, I could use the new LINQ to SQL object relational mapper (ORM) support provided in Orcas to model the SQL Northwind database with classes like below (please watch my video here to learn how to-do this):LINQ的美在于我可以对任何类型的数据使用完全一致的查询语法。比如,我可以用在Orcas中提供的新的LINQ to SQL ORM来构建SQL 的Northwind数据库,如下图:(请看我的视频来学习如何做:watch my video here ):Once Ive defined the class model above (and its mapping to/from the database), I can then write a query syntax expression to fetch all products whose unitprice is greater than $99:一旦我定义了如上的类模型(和它的 从/向 数据库中绘制出来的图),我可以写一个查询语句,查找出unitprice比99$多的产品来:In the above code snippet I am indicating that I want to perform a LINQ query against the Products table on the NorthwindDataContext class created by the ORM designer in Visual Studio Orcas. The select p indicates that I want to return a sequence of Product objects that match my query. The datatype result of this query syntax expression is consequently of type IEnumerable.在上边的代码片断中,我指明了我想对用ORM设计器生成的NorthwindDataContext类的Products表进行查询。select p表达了我想返回跟我的查询想匹配的Product对象。因此,该查询表达式的返回值类型为IEnumerable.Just like with the previous List collection query syntax example, the C# compiler will translate our declarative query syntax into explicit extension method invocations (using Lambda expressions as the arguments). In the case of the above LINQ to SQL example, these Lambda expressions will then be converted into SQL commands and evaluated within SQL server (so that only those Product rows that match the query are returned to our application). Details on the mechanism that enables this Lambda-SQL conversion can be found in my Lambda Expressions blog post under the Lambda Expression Trees section.就像刚才的查询事例List集合那样,C#编译器将会把我们的声明语句翻译成显示的Extension method的调用(将Lambda表达式作为参数)。在上边的LINQ to SQL事例中,这些Lambda表达式将会被转换成SQL命令,并且在SQL Server中进行查询优化(如此,向我们的应用程序中只返回那些匹配了我们查询条件的Product记录)。关于此Lambda转换为SQL的机制的详细资料,在Lambda Expressions blog post 的Lambda Expression Trees小节下可以找到.Query Syntax - Understanding the Where and OrderBy Clauses:查询语法-了解Where和OrderBy语句:Between the opening from clause and closing select clause of a query syntax expression you can use the most common LINQ query operators to filter and transform the data you are querying. Two of the most common clauses youll end up using are where and orderby. These handle the filtering and ordering of results.在查询表达式的开始的from语句和结束的select语句之间,你可以用许多的LINQ查询操作符来过滤和转换你查询的数据。两个最常用的语句是你可以以“where和orderby结束。它们处理了查询结果的过滤和排序。For example, to return a list of alphabetically descending category names from the Northwind database - filtered to only include those categories where there are more than 5 products associated with the category - we could write the below query syntax that uses LINQ to SQL to query our database:例如,为了返回在Northwind数据库中一个以category名称的字母倒序排列的列表,并查出跟本类别关联的产品记录多于5条的记录集,我们可以按照如下方式来写LINQ to SQL来查询数据库:In the above expression we are adding a where c.Products.Count 5 clause to indicate that we only want to return category names where there are more than 5 products in the category. This takes advantage of the LINQ to SQL ORM mapping association between products and categories in our database. In the above expression I also added a orderby c.CategoryName descending clause to indicat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论