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

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

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

  LINQ to SQL will then generate the below SQL when querying the database using this expression:

  然后,LINQ to SQL在利用该表达式查询数据库时会生成如下的SQL语句:

以下是引用片段:
  SELECT [t0].[CategoryName] FROM [dbo].[Categories] AS [t0]
  WHERE ((
  SELECT COUNT(*)
  FROM [dbo].[Products] AS [t1]
  WHERE [t1].[CategoryID] = [t0].[CategoryID]
  )) > 5
  ORDER BY [t0].[CategoryName] DESC

  Notice how LINQ to SQL is smart and only returns back the single column we need (the categoryname). It also does all of the filtering and ordering in the database layer - which makes it very efficient.

  注意,LINQ to SQL是如何的精炼,并且只返回了我们想要的那一列(categoryname)。它也在数据库这一层就做了过滤和排序,这将它变得十分高效。

  Query Syntax - Transforming Data with Projections

  查询语法:用预测来转换数据

  One of the points I made earlier was that the "select" clause indicates what data you want returned, and what shape it should be in.

  刚才我提到了一点,"select"语句表明了你想返回什么数据,返回的数据是什么形式的。

  For example, if you have a "select p" clause like below - where p is of type Person - then it will return a sequence of Person objects:

  例如,如果有一条如下的"select p"语句-该语句中的p代表的是Person类型--那么它就是返回一个Person对象的序列:

  点击查看大图

  One of the really powerful capabilities provided by LINQ and query syntax is the ability for you to define new classes that are separate from the data being queried, and to then use them to control the shape and structure of the data being returned by the query.

  LINQ及其查询语法提供的一个真正强大的功能是你可以定义跟查询数据相互分离的新类,然后用定义的新类来控制查询返回的数据。

  For example, assume that we define a new "AlternatePerson" class that has a single "FullName" property instead of the separate "FirstName" and "LastName" properties that our origional "Person" class had:

  例如,假如我们定义一个新类"AlternatePerson",和原有类不同的时,它没有"FirstName"和"LastName",只有一个FunName属性:

  I could then use the below LINQ query syntax to query my origional List collection, and transform the results to be a sequence of AlternatePerson objects using the query syntax below:

  我可以用如下的LINQ查询语法来查询出原始的List集合,并将结果集转换为AlternatePerson对象:

  点击查看大图

  Notice how we can use the new Object Initializer syntax I talked about in the first post in my language series to create a new AlternatePerson instance and set its properties within the "select" clause of our expression above. Note also how I am assigning the "FullName" property by concatenating the FirstName and LastName properties of our origional Person class.

  注意我们是如何用我在该系列中首篇帖子中提到的用新的Object Initializer语法 来生成一个新类AlternatePerson的实例,并将它的属性在上边的“select"语句中声明。同样也要注意我如何将用拼接原有的Person类的"FirstName"和"LastName"属性的方法将"FullName"属性指定给AlternatePerson。

  Using Query Syntax Projections with a Database

  和数据库一起使用查询语法投影

  This projection feature ends up being incredibly useful when working with data pulled from a remote data provider like a database, since it provides us with an elegant way to indicate which columns of data our ORM should actually fetch from a database.

  这个投影功能在从远程获取数据时非常有用,比如从数据库中获取数据时,因为它提供给了我们一个简洁的方法来声明哪些字段的数据我们的ORM应该从数据库中取出。

  For example, assume I use the LINQ to SQL ORM provider to model the "Northwind" database with classes like below:

  例如,假如我用LINQ to SQL ORM生成"Northwind"数据库,如下所示:

  

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