LINQ標準查詢操做符(一)——select、SelectMany、Where、OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse

1、投影操做符

1. Select

Select操做符對單個序列或集合中的值進行投影。下面的示例中使用select從序列中返回Employee表的全部列:sql

    //查詢語法
    var query =
        from e in db.Employees
        where e.FirstName.StartsWith("M")
        select e;

生成的sql:
SELECT
    [Extent1].[EmployeeID] AS [EmployeeID],
    [Extent1].[LastName] AS [LastName],
    [Extent1].[FirstName] AS [FirstName],
    [Extent1].[Title] AS [Title],
    [Extent1].[TitleOfCourtesy] AS [TitleOfCourtesy],
    [Extent1].[BirthDate] AS [BirthDate],
    [Extent1].[HireDate] AS [HireDate],
    [Extent1].[Address] AS [Address],
    [Extent1].[City] AS [City],
    [Extent1].[Region] AS [Region],
    [Extent1].[PostalCode] AS [PostalCode],
    [Extent1].[Country] AS [Country],
    [Extent1].[HomePhone] AS [HomePhone],
    [Extent1].[Extension] AS [Extension],
    [Extent1].[Photo] AS [Photo],
    [Extent1].[Notes] AS [Notes],
    [Extent1].[ReportsTo] AS [ReportsTo],
    [Extent1].[PhotoPath] AS [PhotoPath]
    FROM [dbo].[Employees] AS [Extent1]
    WHERE [Extent1].[FirstName] LIKE N'M%'

 

//方法語法
    var q =
        db.Employees
        .Where(e => e.FirstName.StartsWith("M"))
        .Select(e => e);

生成的sql:
SELECT
    [Extent1].[EmployeeID] AS [EmployeeID],
    [Extent1].[LastName] AS [LastName],
    [Extent1].[FirstName] AS [FirstName],
    [Extent1].[Title] AS [Title],
    [Extent1].[TitleOfCourtesy] AS [TitleOfCourtesy],
    [Extent1].[BirthDate] AS [BirthDate],
    [Extent1].[HireDate] AS [HireDate],
    [Extent1].[Address] AS [Address],
    [Extent1].[City] AS [City],
    [Extent1].[Region] AS [Region],
    [Extent1].[PostalCode] AS [PostalCode],
    [Extent1].[Country] AS [Country],
    [Extent1].[HomePhone] AS [HomePhone],
    [Extent1].[Extension] AS [Extension],
    [Extent1].[Photo] AS [Photo],
    [Extent1].[Notes] AS [Notes],
    [Extent1].[ReportsTo] AS [ReportsTo],
    [Extent1].[PhotoPath] AS [PhotoPath]
    FROM [dbo].[Employees] AS [Extent1]
    WHERE [Extent1].[FirstName] LIKE N'M%'
能夠簡寫爲:

var qq =
                    db.Employees
                    .Where(e => e.FirstName.StartsWith("M"))
                    .ToList();

=========================================================數組

固然,你也能夠返回單個列,例如:ui

            var query =
                     from e in db.Employees
                     where e.FirstName.StartsWith("M")
                     select e.FirstName;
            var query1 =
                    db.Employees.Where(e => e.FirstName.StartsWith("M"))
                    .Select(e => e.FirstName);

生成的sql:
SELECT
    [Extent1].[FirstName] AS [FirstName]
    FROM [dbo].[Employees] AS [Extent1]
    WHERE [Extent1].[FirstName] LIKE N'M%'

 若是像上面這樣,只查詢單列,則返回相似於無名稱的數組,並不能用於生成Json。
spa

若是想返回單列匿名類,應3d

            var query1 =
                    db.Employees.Where(e => e.FirstName.StartsWith("M"))
                    .Select(e => new { e.FirstName })
                    .ToList();

返回結果爲:
code

==========================================================對象

你也能夠返回序列中的某幾列,例如:blog

            var query =
                (from e in db.Employees
                 where e.FirstName.StartsWith("M")
                 select new
                 {
                     e.FirstName,
                     e.LastName,
                     e.Title
                 }).ToList();
            var query1 =
                db.Employees.Where(e => e.FirstName.StartsWith("M"))
                .Select(e => new
                {
                    e.FirstName,
                    e.LastName,
                    e.Title
                })
                .ToList();
生成的sql:
SELECT
    1 AS [C1],
    [Extent1].[FirstName] AS [FirstName],
    [Extent1].[LastName] AS [LastName],
    [Extent1].[Title] AS [Title]
    FROM [dbo].[Employees] AS [Extent1]
    WHERE [Extent1].[FirstName] LIKE N'M%'

查詢多列,返回的是一個匿名類排序

 

2. SelectMany

SelectMany操做符提供了將多個from子句組合起來的功能,它將每一個對象的結果合併成單個序列。下面是一個示例:ip

            var query =
                (from e in db.Employees
                 from o in e.Orders
                 select o).ToList();
            //方法語法
            var q =
                db.Employees
                .SelectMany(e => e.Orders)
                .ToList();
生成的sql:
SELECT 
    [Extent1].[OrderID] AS [OrderID], 
    [Extent1].[CustomerID] AS [CustomerID], 
    [Extent1].[EmployeeID] AS [EmployeeID], 
    [Extent1].[OrderDate] AS [OrderDate], 
    [Extent1].[RequiredDate] AS [RequiredDate], 
    [Extent1].[ShippedDate] AS [ShippedDate], 
    [Extent1].[ShipVia] AS [ShipVia], 
    [Extent1].[Freight] AS [Freight], 
    [Extent1].[ShipName] AS [ShipName], 
    [Extent1].[ShipAddress] AS [ShipAddress], 
    [Extent1].[ShipCity] AS [ShipCity], 
    [Extent1].[ShipRegion] AS [ShipRegion], 
    [Extent1].[ShipPostalCode] AS [ShipPostalCode], 
    [Extent1].[ShipCountry] AS [ShipCountry]
    FROM [dbo].[Orders] AS [Extent1]
    WHERE [Extent1].[EmployeeID] IS NOT NULL

場景:Employee與Order爲0或1對多的關係,也就是Order的EmployeeID能夠爲null

從生成的sql語句也能夠看到,只查詢出了Order的EmployeeID不爲null的全部Order記錄。

 

 

2、限制操做符

Where是限制操做符,它將過濾標準應用在序列上,按照提供的邏輯對序列中的數據進行過濾。

Where操做符不啓動查詢的執行。當開始對序列進行遍歷時查詢纔開始執行,此時過濾條件將被應用到查詢中。Where操做符的使用方法已經在第一節中出現過,這裏再也不冗述。

3、排序操做符

排序操做符,包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。

1. OrderBy

OrderBy操做符將序列中的元素按照升序排列。下面的示例演示了這一點:

 

            //查詢語法
            var query =
                (from e in db.Employees
                 orderby e.FirstName
                 select e).ToList();
            //方法語法
            var q =
                db.Employees
                .OrderBy(e => e.FirstName)
                .ToList();
生成的sql:
SELECT 
    [Extent1].[EmployeeID] AS [EmployeeID], 
    [Extent1].[LastName] AS [LastName], 
    [Extent1].[FirstName] AS [FirstName], 
    [Extent1].[Title] AS [Title], 
    [Extent1].[TitleOfCourtesy] AS [TitleOfCourtesy], 
    [Extent1].[BirthDate] AS [BirthDate], 
    [Extent1].[HireDate] AS [HireDate], 
    [Extent1].[Address] AS [Address], 
    [Extent1].[City] AS [City], 
    [Extent1].[Region] AS [Region], 
    [Extent1].[PostalCode] AS [PostalCode], 
    [Extent1].[Country] AS [Country], 
    [Extent1].[HomePhone] AS [HomePhone], 
    [Extent1].[Extension] AS [Extension], 
    [Extent1].[Photo] AS [Photo], 
    [Extent1].[Notes] AS [Notes], 
    [Extent1].[ReportsTo] AS [ReportsTo], 
    [Extent1].[PhotoPath] AS [PhotoPath]
    FROM [dbo].[Employees] AS [Extent1]
    ORDER BY [Extent1].[FirstName] ASC

由於未使用Select,因此返回的爲動態包裝類。
這裏可使用OrderBy的重載方法OrderBy(Func<T,TKey>,IComparer<Tkey>)來指定序列的排序方式。

2. OrderByDescending

OrderByDescending操做符將序列中的元素按照降序排列。用法與OrderBy相同,這裏再也不演示。

3. ThenBy

ThenBy操做符實現按照次關鍵字對序列進行升序排列。此操做符的查詢語法與方法語法略有不一樣,如下代碼演示了這一點:

            //查詢語法
            var query =
                (from e in db.Employees
                 orderby e.FirstName, e.LastName
                 select e).ToList();
            //方法語法
            var q =
                db.Employees
                .OrderBy(e => e.FirstName)
                .ThenBy(e => e.LastName)
                .ToList();

生成的sql:
SELECT 
    [Extent1].[EmployeeID] AS [EmployeeID], 
    [Extent1].[LastName] AS [LastName], 
    [Extent1].[FirstName] AS [FirstName], 
    [Extent1].[Title] AS [Title], 
    [Extent1].[TitleOfCourtesy] AS [TitleOfCourtesy], 
    [Extent1].[BirthDate] AS [BirthDate], 
    [Extent1].[HireDate] AS [HireDate], 
    [Extent1].[Address] AS [Address], 
    [Extent1].[City] AS [City], 
    [Extent1].[Region] AS [Region], 
    [Extent1].[PostalCode] AS [PostalCode], 
    [Extent1].[Country] AS [Country], 
    [Extent1].[HomePhone] AS [HomePhone], 
    [Extent1].[Extension] AS [Extension], 
    [Extent1].[Photo] AS [Photo], 
    [Extent1].[Notes] AS [Notes], 
    [Extent1].[ReportsTo] AS [ReportsTo], 
    [Extent1].[PhotoPath] AS [PhotoPath]
    FROM [dbo].[Employees] AS [Extent1]
    ORDER BY [Extent1].[FirstName] ASC, [Extent1].[LastName] ASC

4. ThenByDescending

ThenByDescending操做符實現按照次關鍵字對序列進行降序排列。此操做符的查詢語法與方法語法略有不一樣,如下代碼演示了這一點:

    //查詢語法
    var query =
        from e in db.Employees
        orderby e.FirstName,e.LastName descending
        select e;
    //方法語法
    var q =
        db.Employees
        .OrderBy(e => e.FirstName)
        .ThenByDescending(e => e.LastName)
        .Select(e => e);
    foreach (var item in query)
    {
        Console.WriteLine(item.FirstName);
    }

    生成的sql:

SELECT
    [Extent1].[EmployeeID] AS [EmployeeID],
    [Extent1].[LastName] AS [LastName],
    [Extent1].[FirstName] AS [FirstName],
    [Extent1].[Title] AS [Title],
    [Extent1].[TitleOfCourtesy] AS [TitleOfCourtesy],
    [Extent1].[BirthDate] AS [BirthDate],
    [Extent1].[HireDate] AS [HireDate],
    [Extent1].[Address] AS [Address],
    [Extent1].[City] AS [City],
    [Extent1].[Region] AS [Region],
    [Extent1].[PostalCode] AS [PostalCode],
    [Extent1].[Country] AS [Country],
    [Extent1].[HomePhone] AS [HomePhone],
    [Extent1].[Extension] AS [Extension],
    [Extent1].[Photo] AS [Photo],
    [Extent1].[Notes] AS [Notes],
    [Extent1].[ReportsTo] AS [ReportsTo],
    [Extent1].[PhotoPath] AS [PhotoPath]
    FROM [dbo].[Employees] AS [Extent1]
    ORDER BY [Extent1].[FirstName] ASC, [Extent1].[LastName] DESC

 

5. Reverse

Reverse將會把序列中的元素按照從後到前的循序反轉。須要注意的是,Reverse方法的返回值是void,如下代碼演示了這一點

            //方法語法
            var q =
                db.Employees
                .Select(e => e.FirstName)
                .ToList();
            q.Reverse();
相關文章
相關標籤/搜索