LINQ標准查詢操作符(二)——Join、GroupJoin、GroupBy、Concat、


四、聯接操作符

聯接是指將一個數據源對象與另一個數據源對象進行關聯或者聯合的操作。這兩個數據源對象通過一個共同的值或者屬性進行關聯。

LINQ有兩個聯接操作符:Join和GroupJoin。

1. Join

Join操作符類似於T-SQL中的inner join,它將兩個數據源相聯接,根據兩個數據源中相等的值進行匹配。例如,可以將產品表與產品類別表相聯接,得到產品名稱和與其相對應的類別名稱。以下的代碼演示了這一點:

            //查詢語法
            var query =
                (from p in db.Products
                 join c in db.Categories on p.CategoryID equals c.CategoryID
                 where p.CategoryID == 1
                 select new { p.ProductID, p.ProductName, c.CategoryID, c.CategoryName }).ToList();

生成的sql:
SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[ProductName] AS [ProductName], 
    [Extent2].[CategoryID] AS [CategoryID], 
    [Extent2].[CategoryName] AS [CategoryName]
    FROM  [dbo].[Products] AS [Extent1]
    INNER JOIN [dbo].[Categories] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]
    WHERE (1 = [Extent1].[CategoryID]) AND ([Extent1].[CategoryID] IS NOT NULL)

            //方法語法
            var q =
                db.Products
                .Join
                (
                    db.Categories,
                    p => p.CategoryID,
                    c => c.CategoryID,
                    (p, c) => new { p.ProductID, p.ProductName, c.CategoryID, c.CategoryName }
                )
                .Where(p => p.CategoryID == 1)
                .ToList();

   生成的sql:
SELECT
    [Extent1].[ProductID] AS [ProductID],
    [Extent1].[ProductName] AS [ProductName],
    [Extent2].[CategoryID] AS [CategoryID],
    [Extent2].[CategoryName] AS [CategoryName]
    FROM  [dbo].[Products] AS [Extent1]
    INNER JOIN [dbo].[Categories] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]
    WHERE 1 = [Extent2].[CategoryID]

以上代碼為表述清晰加入了一個條件“where p.CategoryID == 1”,即僅返回產品類別ID為1的所有產品。

生成的sql語句略有不同。

2. GroupJoin

GroupJoin操作符常應用於返回“主鍵對象-外鍵對象集合”形式的查詢,例如“產品類別-此類別下的所有產品”。以下的代碼演示了這一點:

            //查詢語法
            var query =
                (from c in db.Categories
                 join p in db.Products on c.CategoryID equals p.CategoryID into r
                 select new
                 {
                     c.CategoryName,
                     Products = r
                 }).ToList();
            //方法語法
            var q =
                db.Categories
                .GroupJoin
                (
                   db.Products,
                   c => c.CategoryID,
                   p => p.CategoryID,
                   (c, p) => new
                   {
                       c.CategoryName,
                       Products = p
                   }
                )
                .ToList();

生成的sql:
SELECT 
    [Project1].[CategoryID] AS [CategoryID], 
    [Project1].[CategoryName] AS [CategoryName], 
    [Project1].[C1] AS [C1], 
    [Project1].[ProductID] AS [ProductID], 
    [Project1].[ProductName] AS [ProductName], 
    [Project1].[SupplierID] AS [SupplierID], 
    [Project1].[CategoryID1] AS [CategoryID1], 
    [Project1].[QuantityPerUnit] AS [QuantityPerUnit], 
    [Project1].[UnitPrice] AS [UnitPrice], 
    [Project1].[UnitsInStock] AS [UnitsInStock], 
    [Project1].[UnitsOnOrder] AS [UnitsOnOrder], 
    [Project1].[ReorderLevel] AS [ReorderLevel], 
    [Project1].[Discontinued] AS [Discontinued]
    FROM ( SELECT 
        [Extent1].[CategoryID] AS [CategoryID], 
        [Extent1].[CategoryName] AS [CategoryName], 
        [Extent2].[ProductID] AS [ProductID], 
        [Extent2].[ProductName] AS [ProductName], 
        [Extent2].[SupplierID] AS [SupplierID], 
        [Extent2].[CategoryID] AS [CategoryID1], 
        [Extent2].[QuantityPerUnit] AS [QuantityPerUnit], 
        [Extent2].[UnitPrice] AS [UnitPrice], 
        [Extent2].[UnitsInStock] AS [UnitsInStock], 
        [Extent2].[UnitsOnOrder] AS [UnitsOnOrder], 
        [Extent2].[ReorderLevel] AS [ReorderLevel], 
        [Extent2].[Discontinued] AS [Discontinued], 
        CASE WHEN ([Extent2].[ProductID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
        FROM  [dbo].[Categories] AS [Extent1]
        LEFT OUTER JOIN [dbo].[Products] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]
    )  AS [Project1]
    ORDER BY [Project1].[CategoryID] ASC, [Project1].[C1] ASC

返回的結果為:

 

 

 

五、分組操作符

分組是根據一個特定的值將序列中的元素進行分組。LINQ只包含一個分組操作符:GroupBy

下面的示例中使用了產品表,以CategoryID作為分組關鍵值,按照產品類別對產品進行了分組。

 

            //查詢語法
            var query =
                (from p in db.Products
                 group p by p.CategoryID).ToList();
            //方法語法
            var q =
                db.Products
                .GroupBy(p => p.CategoryID)
                .ToList();

生成的sql:
SELECT 
    [Project2].[C1] AS [C1], 
    [Project2].[CategoryID] AS [CategoryID], 
    [Project2].[C2] AS [C2], 
    [Project2].[ProductID] AS [ProductID], 
    [Project2].[ProductName] AS [ProductName], 
    [Project2].[SupplierID] AS [SupplierID], 
    [Project2].[CategoryID1] AS [CategoryID1], 
    [Project2].[QuantityPerUnit] AS [QuantityPerUnit], 
    [Project2].[UnitPrice] AS [UnitPrice], 
    [Project2].[UnitsInStock] AS [UnitsInStock], 
    [Project2].[UnitsOnOrder] AS [UnitsOnOrder], 
    [Project2].[ReorderLevel] AS [ReorderLevel], 
    [Project2].[Discontinued] AS [Discontinued]
    FROM ( SELECT 
        [Distinct1].[CategoryID] AS [CategoryID], 
        1 AS [C1], 
        [Extent2].[ProductID] AS [ProductID], 
        [Extent2].[ProductName] AS [ProductName], 
        [Extent2].[SupplierID] AS [SupplierID], 
        [Extent2].[CategoryID] AS [CategoryID1], 
        [Extent2].[QuantityPerUnit] AS [QuantityPerUnit], 
        [Extent2].[UnitPrice] AS [UnitPrice], 
        [Extent2].[UnitsInStock] AS [UnitsInStock], 
        [Extent2].[UnitsOnOrder] AS [UnitsOnOrder], 
        [Extent2].[ReorderLevel] AS [ReorderLevel], 
        [Extent2].[Discontinued] AS [Discontinued], 
        CASE WHEN ([Extent2].[ProductID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
        FROM   (SELECT DISTINCT 
            [Extent1].[CategoryID] AS [CategoryID]
            FROM [dbo].[Products] AS [Extent1] ) AS [Distinct1]
        LEFT OUTER JOIN [dbo].[Products] AS [Extent2] ON ([Distinct1].[CategoryID] = [Extent2].[CategoryID]) OR (([Distinct1].[CategoryID] IS NULL) AND ([Extent2].[CategoryID] IS NULL))
    )  AS [Project2]
    ORDER BY [Project2].[CategoryID] ASC, [Project2].[C2] ASC

執行GroupBy得到的序列中包含的元素類型為IGrouping<TKey, T>,其Key屬性代表了分組時使用的關鍵值,遍歷IGrouping<TKey, T>元素可以讀取到每一個T類型。在此示例中,對應的元素類型為IGrouping<int, Products>,其Key屬性即為類別ID,遍歷它可以讀取到每一個產品對象。

六、串聯操作符

串聯是一個將兩個集合聯接在一起的過程。在LINQ中,這個過程通過Concat操作符來實現。

在下面的示例中,將會把類別名稱串聯在產品名稱之后:

//方法語法
            var q =
                db.Products
                .Select(p => p.ProductName)
                .Concat
               (
                    db.Categories.Select(c => c.CategoryName)
                )
                .ToList();

生成的sql:

SELECT 
    [UnionAll1].[ProductName] AS [C1]
    FROM  (SELECT 
        [Extent1].[ProductName] AS [ProductName]
        FROM [dbo].[Products] AS [Extent1]
    UNION ALL
        SELECT 
        [Extent2].[CategoryName] AS [CategoryName]
        FROM [dbo].[Categories] AS [Extent2]) AS [UnionAll1]

返回結果77+8=85

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM