热门问题
时间线
聊天
视角

语言集成查询

一項為.Net提供SQL自然查詢語法的技術 来自维基百科,自由的百科全书

Remove ads

语言集成查询(英语:Language Integrated Query缩写:LINQ),发音"link",是微软的一项技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,目前可支持C#以及Visual Basic .NET语言。2007年11月19日随.NET Framework 3.5发布了LINQ技术。

包括LINQ to Objects、LINQ to SQL、LINQ to Datasets、LINQ to Entities、LINQ to Data Source、LINQ to XML/XSD等。

语言风格

LINQ依赖于语言的多项新增风格,来展示出查询语言的扩展性。例如:C#:

匿名类型

匿名类型(Anonymous type)是C# 3.0与Visual Basic 9.0新增的功能,它允许开发人员可以使用不具类型的方式建立新的数据结构,而真正的类型在编译时期,由C# (或VB) Compiler自动产生,并写入编译目标文件中,它可以让开发人员能够很简单利用匿名类型建立对象,LINQ中的select指令即是利用这种特性来建立回传对象。

匿名类型本质上是表达元组(tuple),采用值语义。

下列使用匿名类型的代码:

    [WebGet]
    public IQueryable<Categories> GetCategoryByName(string CategoryName)
    {
        try
        {
            var query = base.CurrentDataSource.Categories.Where
         ("it.CategoryName = @Name", new ObjectParameter[] { new ObjectParameter("Name", CategoryName) });
        }
        catch (Exception)
        {
            throw;
        }
        return query;
    }

会由编译器改写为:

    [WebGet]
    public IQueryable<Categories> GetCategoryByName(string CategoryName)
    {
        IQueryable<Categories> CS$1$0000; // 由編譯器改寫而成。
        try
        {
            CS$1$0000 = base.CurrentDataSource.Categories.Where
         ("it.CategoryName = @Name", new ObjectParameter[] { new ObjectParameter("Name", CategoryName) });
        }
        catch (Exception)
        {
            throw;
        }
        return CS$1$0000;
    }
Remove ads

扩展方法 (Extension method)

Lambda表达式 (Lambda expression)

表达式树 (Expression tree)

查询表达式语法

from RangeVariable in IEnumerable<T>或IQueryable<T>的Collection
<Standard Query  Operators> <lambda expression>
<select or groupBy operator> <result   formation>

流利语法

LINQ查询时有两种语法可供选择:查询表达式语法(Query Expression)和流利语法(Fluent Syntax)。前者使用查询运算符;后者利用System.Linq.Enumerable类中定义的扩展方法和Lambda表达式方式进行查询。CLR本身并不理解查询表达式语法,它只理解流利语法。编译器负责把查询表达式语法编译为流利语法。

以下是一个示例LINQ方法语法的查询,返回数组中的偶数:

int[] ints={1,2,3,4};
var result = ints.Where(p => p % 2 == 0).ToArray();

对比流利语法和C#的传统语法:

//   extension methods make LINQ elegant
            IEnumerable<string> query = names
                .Where(n => n.Contains("a"))
                .OrderBy(n => n.Length)
                .Select(n => n.ToUpper());
 
//   static methods lose query's fluency
            IEnumerable<string> query2 =
                Enumerable.Select(
                    Enumerable.OrderBy(
                        Enumerable.Where(names, n => n.Contains("a")
                        ), n => n.Length
                    ), n => n.ToUpper()
                );
Remove ads

标准查询运算符 (Standard query operators)

System.Linq.Enumerable静态类声明了一套标准查询操作符(Standard Query Operators,SQO)方法集合。基本语法如下:

 using (var db = new EntityContext())
 {
     var roles = from o in db.Users
                where o.Account == "Apollo"
                select o.Roles;
     
 }

标准查询操作符和Lambda表达式的关系非常密切。编译器会将上述表达式转化为下述以Lambda表达式为参数的显式扩展方法调用序列:

 using (var db = new EntityContext())
 {
     var roles = db.Users.Where(o => o.Account == "Apollo").Select(o => o.Roles);
 }
更多信息 操作符, 类别 ...
Remove ads

LINQ的各式言语支持度

下列的言语支持LINQ。

注:C++/CLI尚未支持LINQ。但是有第三方的C++包[1],以及第三方的PHP[2]

LINQ的示例

一个简单例子:

using System;
using System.Linq;

namespace DuckTyping
{
    internal class Program
    {
        private static void Main()
        {
            int[] array = { 1, 5, 2, 10, 7 };  
            // Select squares of all odd numbers in the array sorted in descending order
            var results = from x in array
                                     where x % 2 == 1
                                     orderby x descending
                                     select x * x;
            foreach (var result in results)
            {
                Console.WriteLine(result); 
            }
        }
    }
}

输出: 49 25 1

另一个例子:

// the Northwind type is a subclass of DataContext created by SQLMetal
// Northwind.Orders is of type Table<Order>
// Northwind.Customers is of type Table<Customer>

Northwind db = new Northwind(connectionString);
 
// use 'var' keyword because there is no name for the resultant type of the projection
 
var q =  from o in db.Orders
         from c in db.Customers
         where o.Quality == "200" && (o.CustomerID == c.CustomerID)
         select new { o.DueDate, c.CompanyName, c.ItemID, c.ItemName };
 
// q is now an IEnumerable<T>, where T is the anonymous type generated by the compiler

foreach (var t in q)
{
    // t is strongly typed, even if we can't name the type at design time
 
    Console.WriteLine("DueDate Type = {0}", t.DueDate.GetType());
    Console.WriteLine("CompanyName (lowercased) = {0}", t.CompanyName.ToLower());
    Console.WriteLine("ItemID * 2 = {0}", t.ItemID * 2);
}
Remove ads

Visual Studio支持

LINQ目前由Visual Studio 2008、2010、2012、2013、2015、2017、2019支持。

语言扩展

微软同样提供了LINQExtender,允许用户在不了解LINQ实现细节的情况下,编写自己的LINQ扩展。 如:LINQ to Twitter,LINQ to Oracle,LINQ to Active Directory等

参考文献

外部链接

参见

Loading related searches...

Wikiwand - on

Seamless Wikipedia browsing. On steroids.

Remove ads