语言集成查询
语言风格
LINQ新增了多项语言的风格,来展示出查询语言的扩充性,例如:C#:
匿名类型
匿名类型(Anonymous type)是C# 3.0与Visual Basic 9.0新增的功能,它允许开发人员可以使用不具类型的方式创建新的数据结构,而真正的类型在编译时期,由C# (或VB) Compiler自动产生,并写入编译目标文件中,它可以让开发人员能够很简单利用匿名类型创建对象,LINQ中的select指令即是利用这种特性来创建回传对象。
匿名类型本质上是表达元组(tuple),采用值语义。
下列使用匿名类型的代码:
[WebGet]publicIQueryableGetCategoryByName(stringCategoryName){try{varquery=base.CurrentDataSource.Categories.Where("it.CategoryName = @Name",newObjectParameter[]{newObjectParameter("Name",CategoryName)});}catch(Exception){throw;}returnquery;}
会由编译器改写为:
[WebGet]publicIQueryableGetCategoryByName(stringCategoryName){IQueryableCS$1$0000;// 由編譯器改寫而成。try{CS$1$0000=base.CurrentDataSource.Categories.Where("it.CategoryName = @Name",newObjectParameter[]{newObjectParameter("Name",CategoryName)});}catch(Exception){throw;}returnCS$1$0000;}
扩展方法 (Extension method)
Lambda表达式 (Lambda expression)
表达式树 (Expression tree)
标准查询运算符 (Standard query operators)
LINQ的各式言语支持度
下列的言语支持LINQ。
C# 3.0
F# 1.1.8.1
Visual Basic 2008(9.0)
注:C++/CLI尚未支持LINQ。但是有第三方的C++包 ,以及第三方的PHP包
LINQ的示例
一个简单例子:
usingSystem;usingSystem.Linq;namespaceDuckTyping{internalclassProgram{privatestaticvoidMain(){int[]array={1,5,2,10,7};// Select squares of all odd numbers in the array sorted in descending ordervarresults=fromxinarraywherex%2==1orderbyxdescendingselectx*x;foreach(varresultinresults){Console.WriteLine(result);}}}}
输出: 49 25 1
另一个例子:
// the Northwind type is a subclass of DataContext created by SQLMetal// Northwind.Orders is of type Table// Northwind.Customers is of type TableNorthwinddb=newNorthwind(connectionString);// use "var" keyword because there is no name for the resultant type of the projectionvarq=fromoindb.Ordersfromcindb.Customerswhereo.Quality=="200"&&(o.CustomerID==c.CustomerID)selectnew{o.DueDate,c.CompanyName,c.ItemID,c.ItemName};// q is now an IEnumerable, where T is the anonymous type generated by the compilerforeach(vartinq){// t is strongly typed, even if we can"t name the type at design timeConsole.WriteLine("DueDate Type = {0}",t.DueDate.GetType());Console.WriteLine("CompanyName (lowercased) = {0}",t.CompanyName.ToLower());Console.WriteLine("ItemID * 2 = {0}",t.ItemID*2);}
Visual Studio支持
LINQ目前由Visual Studio 2008、2010、2012、2013、2015支持。
语言扩展
微软同样提供了LINQExtender,允许用户在不了解LINQ实现细节的情况下,编写自己的LINQ扩展。 如:LINQ to Twitter,LINQ to Oracle,LINQ to Active Directory等
相关
对象关系映射(ORM)
免责声明:以上内容版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。感谢每一位辛勤著写的作者,感谢每一位的分享。
![](https://imgs1.zupu.cn/static/web/img/toplogin.png)
- 有价值
- 一般般
- 没价值
![](https://imgs0.zupu.cn/photos/common/20210831/5f77025c-05aa-4528-8ff4-390397a5720d.png)
![](https://imgs0.zupu.cn/photos/common/20210831/fc60bb85-0172-4554-b1b5-84e226beefd2.png)
![](https://imgs0.zupu.cn/photos/common/20210831/77b1b221-2263-4a50-a438-3fe70c458147.png)
![](https://imgs0.zupu.cn/photos/common/20210901/bf46d3b7-c6b5-4a58-ae45-919cadfc8f58.png)
![](https://imgs0.zupu.cn/photos/common/20210903/71ed74ca-9551-4d33-913e-aed4f1956e48.jpg)
![](https://imgs0.zupu.cn/photos/common/20210901/bf46d3b7-c6b5-4a58-ae45-919cadfc8f58.png)
![](https://imgs0.zupu.cn/photos/common/20210901/106cf47a-2bf9-43b3-8b6f-76bb2958edd9.png)
![](https://imgs0.zupu.cn/photos/common/20210903/71ed74ca-9551-4d33-913e-aed4f1956e48.jpg)
24小时热门
推荐阅读
![](https://imgs0.zupu.cn/news/2020/10/19/372/ddade164-5a9e-4c93-8d16-08904e2ce29c.jpg)
![](https://imgs0.zupu.cn/photos/common/2023/05/15/7e6aca87-1de7-4188-b417-4cab8b9aa924.png)
关于我们
![](https://imgs0.zupu.cn/photos/common/20210901/fc6ee093-f219-47fc-90da-21bd9721b53d.jpg)
APP下载
![](https://imgs0.zupu.cn/photos/common/20210901/ea3c7971-1e11-4045-b81c-880d962d4986.png)
![](https://imgs0.zupu.cn/photos/common/20201105/f86bb195-6306-4041-b306-d17003e00182.png)
{{item.time}} {{item.replyListShow ? '收起' : '展开'}}评论 {{curReplyId == item.id ? '取消回复' : '回复'}}