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

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 don't want to return back a sequence of Person objects - but rather I want to return a sequence of strings - populated from each Person object's 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 I've 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 you'll 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 indicate that I want to sort the results in descending order.

  在上边的表达戒,我们增加了"where c.Products.Count > 5"表达式来表明我们只想返回该类别的产品记录数大于5的类别列表。这利用了数据库中的products表和categories表之间的联系。在上边的表达式中我还加了"orderby c.CategoryName descending"语句来表达我想将产品结果倒序排列。

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