<a href="http://webabcd.blog.51cto.com/1787395/341154" target="_blank">[索引頁]</a>
<a href="http://down.51cto.com/data/99915">[源碼下載下傳]</a>
再接再厲VS 2008 sp1 + .NET 3.5 sp1(2) - Entity Framework(實體架構)之詳解 Linq To Entities 之一
介紹
以Northwind為示例資料庫,ADO.NET Entity Framework之Linq To Entities
First - 傳回集合中的第一個成員;不延遲
FirstOrDefault - 傳回集合中的第一個成員(找不到則傳回null);不延遲
All - 是否集合中所有成員都滿足某一條件;不延遲
Any - 集合中是否有成員滿足某一條件;不延遲
Average - 取平均值;不延遲
Sum - 求和;不延遲
Max - 取最大值;不延遲
Min - 取最小值;不延遲
Count - 取指定集合的成員數,傳回值類型int;不延遲
LongCount - 取指定集合的成員數,傳回值類型long;不延遲
Take - 擷取集合的前 n 個成員;延遲
Skip - 跳過集合的前 n 個成員;延遲(Linq To Entities 需要先排序才能 Skip)
Distinct - 過濾集合中的相同項;延遲
Union - 連接配接不同集合,自動過濾相同項;延遲
UnionAll - 連接配接不同集合,不會自動過濾相同項;延遲
Concat - 連接配接不同集合,不會自動過濾相同項;延遲
Intersect - 擷取不同集合的相同項(交集);延遲
Except - 從某集合中删除其與另一個集合中相同的項;延遲
示例
First
using (var ctx = new NorthwindEntities())
{
Products first = ctx.Products.First(p => p.ProductID > 3);
}
SELECT
[Limit1].[C1] AS [C1],
[Limit1].[Discontinued] AS [Discontinued],
[Limit1].[ProductID] AS [ProductID],
[Limit1].[ProductName] AS [ProductName],
[Limit1].[QuantityPerUnit] AS [QuantityPerUnit],
[Limit1].[ReorderLevel] AS [ReorderLevel],
[Limit1].[UnitPrice] AS [UnitPrice],
[Limit1].[UnitsInStock] AS [UnitsInStock],
[Limit1].[UnitsOnOrder] AS [UnitsOnOrder],
[Limit1].[CategoryID] AS [CategoryID],
[Limit1].[SupplierID] AS [SupplierID]
FROM ( SELECT TOP (1)
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[Discontinued] AS [Discontinued],
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[QuantityPerUnit] AS [QuantityPerUnit],
[Extent1].[ReorderLevel] AS [ReorderLevel],
[Extent1].[SupplierID] AS [SupplierID],
[Extent1].[UnitPrice] AS [UnitPrice],
[Extent1].[UnitsInStock] AS [UnitsInStock],
[Extent1].[UnitsOnOrder] AS [UnitsOnOrder],
1 AS [C1]
FROM [dbo].[Products] AS [Extent1]
WHERE [Extent1].[ProductID] > 3
) AS [Limit1]
FirstOrDefault
Products firstOrDefault = ctx.Products.FirstOrDefault(p => p.ProductID > 100);
WHERE [Extent1].[ProductID] > 100
All
bool all = ctx.Products.All(p => p.ProductID > 3);
CASE WHEN ( NOT EXISTS (SELECT
cast(1 as bit) AS [C1]
WHERE ( NOT ([Extent1].[ProductID] > 3)) OR (CASE WHEN ([Extent1].[ProductID] > 3) THEN cast(1 as bit) WHEN ( NOT ([Extent1].[ProductID] > 3)) THEN cast(0 as bit) END IS NULL)
)) THEN cast(1 as bit) WHEN ( EXISTS (SELECT
FROM [dbo].[Products] AS [Extent2]
WHERE ( NOT ([Extent2].[ProductID] > 3)) OR (CASE WHEN ([Extent2].[ProductID] > 3) THEN cast(1 as bit) WHEN ( NOT ([Extent2].[ProductID] > 3)) THEN cast(0 as bit) END IS NULL)
)) THEN cast(0 as bit) END AS [C1]
FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
Any
bool any = ctx.Products.Any(p => p.ProductID > 3);
CASE WHEN ( EXISTS (SELECT
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
WHERE [Extent2].[ProductID] > 3
Average
decimal? average = ctx.Products.Average(p => p.UnitPrice);
[GroupBy1].[A1] AS [C1]
FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
LEFT OUTER JOIN (SELECT
AVG([Extent1].[UnitPrice]) AS [A1]
FROM [dbo].[Products] AS [Extent1] ) AS [GroupBy1] ON 1 = 1
Sum
decimal? sum = ctx.Products.Sum(p => p.UnitPrice);
SUM([Extent1].[UnitPrice]) AS [A1]
Max
decimal? max = ctx.Products.Max(p => p.UnitPrice);
MAX([Extent1].[UnitPrice]) AS [A1]
Min
decimal? min = ctx.Products.Min(p => p.UnitPrice);
MIN([Extent1].[UnitPrice]) AS [A1]
Count
int count = ctx.Products.Count(p => p.ProductID > 3);
COUNT(cast(1 as bit)) AS [A1]
WHERE [Extent1].[ProductID] > 3 ) AS [GroupBy1] ON 1 = 1
LongCount
long longCount = ctx.Products.LongCount(p => p.ProductID > 3);
COUNT_BIG(cast(1 as bit)) AS [A1]
Take
IQueryable<Products> take = ctx.Products.Take(3);
take.ToList();
FROM ( SELECT TOP (3)
Skip
IQueryable<Products> skip = ctx.Products.OrderBy(p => p.UnitPrice).Skip(3);
skip.ToList();
[Project1].[C1] AS [C1],
[Project1].[Discontinued] AS [Discontinued],
[Project1].[ProductID] AS [ProductID],
[Project1].[ProductName] AS [ProductName],
[Project1].[QuantityPerUnit] AS [QuantityPerUnit],
[Project1].[ReorderLevel] AS [ReorderLevel],
[Project1].[UnitPrice] AS [UnitPrice],
[Project1].[UnitsInStock] AS [UnitsInStock],
[Project1].[UnitsOnOrder] AS [UnitsOnOrder],
[Project1].[CategoryID] AS [CategoryID],
[Project1].[SupplierID] AS [SupplierID]
FROM ( SELECT [Project1].[CategoryID] AS [CategoryID], [Project1].[Discontinued] AS [Discontinued], [Project1].[ProductID] AS [ProductID], [Project1].[ProductName] AS [ProductName], [Project1].[QuantityPerUnit] AS [QuantityPerUnit], [Project1].[ReorderLevel] AS [ReorderLevel], [Project1].[SupplierID] AS [SupplierID], [Project1].[UnitPrice] AS[UnitPrice], [Project1].[UnitsInStock] AS [UnitsInStock], [Project1].[UnitsOnOrder] AS [UnitsOnOrder], [Project1].[C1] AS [C1], row_number() OVER (ORDER BY [Project1].[UnitPrice] ASC) AS [row_number]
FROM ( SELECT
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[Discontinued] AS [Discontinued],
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[QuantityPerUnit] AS [QuantityPerUnit],
[Extent1].[ReorderLevel] AS [ReorderLevel],
[Extent1].[SupplierID] AS [SupplierID],
[Extent1].[UnitPrice] AS [UnitPrice],
[Extent1].[UnitsInStock] AS [UnitsInStock],
[Extent1].[UnitsOnOrder] AS [UnitsOnOrder],
1 AS [C1]
FROM [dbo].[Products] AS [Extent1]
) AS [Project1]
) AS [Project1]
WHERE [Project1].[row_number] > 3
ORDER BY [Project1].[UnitPrice] ASC
Distinct
ObjectQuery<Products> distinct = ctx.Products.Distinct();
distinct.ToList();
1 AS [C1],
[Extent1].[Discontinued] AS [Discontinued],
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[QuantityPerUnit] AS [QuantityPerUnit],
[Extent1].[ReorderLevel] AS [ReorderLevel],
[Extent1].[UnitPrice] AS [UnitPrice],
[Extent1].[UnitsInStock] AS [UnitsInStock],
[Extent1].[UnitsOnOrder] AS [UnitsOnOrder],
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[SupplierID] AS [SupplierID]
FROM [dbo].[Products] AS [Extent1]
Union
ObjectQuery<Products> union = ctx.Products.Union(ctx.Products);
union.ToList();
[Distinct1].[C1] AS [C2],
[Distinct1].[C2] AS [C3],
[Distinct1].[C3] AS [C4],
[Distinct1].[C4] AS [C5],
[Distinct1].[C5] AS [C6],
[Distinct1].[C6] AS [C7],
[Distinct1].[C7] AS [C8],
[Distinct1].[C8] AS [C9],
[Distinct1].[C9] AS [C10],
[Distinct1].[C10] AS [C11]
FROM ( SELECT DISTINCT
[UnionAll1].[Discontinued] AS [C1],
[UnionAll1].[ProductID] AS [C2],
[UnionAll1].[ProductName] AS [C3],
[UnionAll1].[QuantityPerUnit] AS [C4],
[UnionAll1].[ReorderLevel] AS [C5],
[UnionAll1].[UnitPrice] AS [C6],
[UnionAll1].[UnitsInStock] AS [C7],
[UnionAll1].[UnitsOnOrder] AS [C8],
[UnionAll1].[CategoryID] AS [C9],
[UnionAll1].[SupplierID] AS [C10]
FROM (SELECT
[Extent1].[SupplierID] AS [SupplierID]
UNION ALL
SELECT
[Extent2].[Discontinued] AS [Discontinued],
[Extent2].[ProductID] AS [ProductID],
[Extent2].[ProductName] AS [ProductName],
[Extent2].[QuantityPerUnit] AS [QuantityPerUnit],
[Extent2].[ReorderLevel] AS [ReorderLevel],
[Extent2].[UnitPrice] AS [UnitPrice],
[Extent2].[UnitsInStock] AS [UnitsInStock],
[Extent2].[UnitsOnOrder] AS [UnitsOnOrder],
[Extent2].[CategoryID] AS [CategoryID],
[Extent2].[SupplierID] AS [SupplierID]
FROM [dbo].[Products] AS [Extent2]) AS [UnionAll1]
) AS [Distinct1]
UnionAll
ObjectQuery<Products> unionAll = ctx.Products.UnionAll(ctx.Products);
unionAll.ToList();
[UnionAll1].[Discontinued] AS [C2],
[UnionAll1].[ProductID] AS [C3],
[UnionAll1].[ProductName] AS [C4],
[UnionAll1].[QuantityPerUnit] AS [C5],
[UnionAll1].[ReorderLevel] AS [C6],
[UnionAll1].[UnitPrice] AS [C7],
[UnionAll1].[UnitsInStock] AS [C8],
[UnionAll1].[UnitsOnOrder] AS [C9],
[UnionAll1].[CategoryID] AS [C10],
[UnionAll1].[SupplierID] AS [C11]
FROM (SELECT
[Extent1].[SupplierID] AS [SupplierID]
UNION ALL
SELECT
[Extent2].[Discontinued] AS [Discontinued],
[Extent2].[ProductID] AS [ProductID],
[Extent2].[ProductName] AS [ProductName],
[Extent2].[QuantityPerUnit] AS [QuantityPerUnit],
[Extent2].[ReorderLevel] AS [ReorderLevel],
[Extent2].[UnitPrice] AS [UnitPrice],
[Extent2].[UnitsInStock] AS [UnitsInStock],
[Extent2].[UnitsOnOrder] AS [UnitsOnOrder],
[Extent2].[CategoryID] AS [CategoryID],
[Extent2].[SupplierID] AS [SupplierID]
FROM [dbo].[Products] AS [Extent2]) AS [UnionAll1]
Concat
IQueryable<Products> concat = ctx.Products.Concat(ctx.Products);
concat.ToList();
Intersect
ObjectQuery<Products> intersect = ctx.Products.Intersect(ctx.Products);
intersect.ToList();
[Intersect1].[Discontinued] AS [C2],
[Intersect1].[ProductID] AS [C3],
[Intersect1].[ProductName] AS [C4],
[Intersect1].[QuantityPerUnit] AS [C5],
[Intersect1].[ReorderLevel] AS [C6],
[Intersect1].[UnitPrice] AS [C7],
[Intersect1].[UnitsInStock] AS [C8],
[Intersect1].[UnitsOnOrder] AS [C9],
[Intersect1].[CategoryID] AS [C10],
[Intersect1].[SupplierID] AS [C11]
INTERSECT
FROM [dbo].[Products] AS [Extent2]) AS [Intersect1]
Except
ObjectQuery<Products> except = ctx.Products.Except(ctx.Products);
except.ToList();
[Except1].[Discontinued] AS [C2],
[Except1].[ProductID] AS [C3],
[Except1].[ProductName] AS [C4],
[Except1].[QuantityPerUnit] AS [C5],
[Except1].[ReorderLevel] AS [C6],
[Except1].[UnitPrice] AS [C7],
[Except1].[UnitsInStock] AS [C8],
[Except1].[UnitsOnOrder] AS [C9],
[Except1].[CategoryID] AS [C10],
[Except1].[SupplierID] AS [C11]
EXCEPT
FROM [dbo].[Products] AS [Extent2]) AS [Except1]
OK
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/341457,如需轉載請自行聯系原作者