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

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

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

  By writing the LINQ query below, I am telling LINQ to SQL that I want a sequence of "Product" objects returned:

  通过写如下的LINQ 查询语句,我告诉LINQ to SQL我想返回一个"Product"对象序列:

  

  All of the columns necessary to populate the Product class would be returned from the database as part of the above query, and the raw SQL executed by the LINQ to SQL ORM would look like below:

  所有的在Product类中的列将会作为以上查询的一部分从数据库返回,并且LINQ to SQL ORM执行的原生的SQL语句是:

以下是引用片段:
  SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID],
  [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock],
  [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
  FROM [dbo].[Products] AS [t0]
  WHERE [t0].[UnitPrice] > 99

  If I didn't need/want all of these columns for some scenarios, I could alternatively define a new "MyProduct" class like below that has a subset of the properties that the Product class has, as well as one additional property - "TotalRevenue" -- that the Product class doesn't have (note: for people not familiar with C#, the Decimal? syntax indicates that UnitPrice property is a nullable value):

  在某些情况下,如果我不需要,也不想返回所有的列,我可以按照如下方式来定义一个新的"MyProduct"类,该类含有Product类含有的的辅助属性,并且也有一个额外的属性”TotalRevenue"--该属性是Product类中的(不熟悉C#语法者:Decimal?表达式表明了UnitPrice属性不可为空值):

  

  I can then use the projection capability of query syntax to shape the data I want returned from the database using a query like below:

  我可以用如下的查询语句来用查询语法的投影功能来将从数据库中返回的数据“格式”一下:

  

  This is indicating that instead of returning a sequence of "Product" objects, I instead want "MyProduct" objects, and that I only need 3 properties of them filled. LINQ to SQL is then smart enough to adjust the raw SQL to execute to only return those three needed product columns from the database:

  这表明了我不想让它返回"Product"对象的序列,取而代之的是我想让它返回“MyProduct"序列,并且我只想让它有3列。LINQ to SQL将会非常智能地调整原来的SQL语句,以使得从数据库中只查询出产品的那3列:

以下是引用片段:
  SELECT [t0].[ProductID], [t0].[ProductName], [t0].[UnitPrice]
  FROM [dbo].[Products] AS [t0]
  WHERE [t0].[UnitPrice] > 99

  Just to show-off, I could also populate the 4th property of the MyProduct class - which is the "TotalRevenue" property. I want this value to be the aggregate amount of revenue that our products have sold for. This value isn't stored anywhere as a pre-computed column within the Northwind database. Instead you need to perform a join between the "Products" table and the "Order Details" table and sum up all of the Order Detail rows associated with a given product.

  仅仅是为了演示一下,我也可以为MyProduct类添加第4个属性--"TotalRevenue",我想用这个属性来合计产品已经卖出的利润。这个不像一个已经定义好的数据列那样存在数据库的中。你需要在"Product"表和"Order Details"表中做一个连接,并将所有和已知产品相关联的Order Details 记录行求个总和。

  What is cool is that I can use the LINQ "Sum" extension method on the Product class's OrderDetails association and write a multiplication Lambda expression as part of my query syntax projection to compute this value:

  酷的是,我可以用LINQ的"Sum" extension method来对Product类的OrderDetails关系做加法去处并且写一个"Lambda"表达式做为我的查询语法投影来计算该值:

  点击查看大图

  LINQ to SQL is then smart enough to use the below SQL to perform the calculation in the SQL database:

  LINQ to SQL 然后就非常聪明地用如下的SQL语句在sql 数据库中进行计算:

以下是引用片段:
  SELECT [t0].[ProductID], [t0].[ProductName], [t0].[UnitPrice], (
  SELECT SUM([t2].[value])
  FROM (
  SELECT [t1].[UnitPrice] * (CONVERT(Decimal(29,4),[t1].[Quantity])) AS [value], [t1].[ProductID]
  FROM [dbo].[Order Details] AS [t1]
  ) AS [t2]
  WHERE [t2].[ProductID] = [t0].[ProductID]
  ) AS [value]
  FROM [dbo].[Products] AS [t0]
  WHERE [t0].[UnitPrice] > 99
网友关注
最新上市
编辑推荐
欢迎订阅天极网RSS聚合资讯:http://www.yesky.com/index.xml