Quantcast
Viewing latest article 20
Browse Latest Browse All 41

LINQ 表示式(5) - Concat、Union、Intersect、Except

關鍵字: Concat、Union、Intersect、Except

這些關鍵字適用於對二個集合做比對處理,例如,合併,交集,差集。

Concat

合併兩個序列,不會自動過濾相同項


(
from c in Customers
where c.ContactName.Contains("ann")
select new { c.CustomerID, c.ContactName }
)
.Concat
(
from c in Customers
where c.ContactName.Contains("von")
select new { c.CustomerID, c.ContactName }
)

Customers
.Where (c => c.ContactName.Contains ("ann"))
.Select ( c => new { c.CustomerID, c.ContactName } )
.Concat (
Customers
.Where (c => c.ContactName.Contains ("von"))
.Select ( c => new { c.CustomerID, c.ContactName } )
)

SELECT [t0].[CustomerID], [t0].[ContactName] FROM [Customers] AS [t0] WHERE [t0].[ContactName] LIKE '%ann%'
UNION ALL
SELECT [t0].[CustomerID], [t0].[ContactName] FROM [Customers] AS [t0] WHERE [t0].[ContactName] LIKE '%von%'

Image may be NSFW.
Clik here to view.

Union

產生兩個序列的聯集(∪)

Image may be NSFW.
Clik here to view.


(
from c in Customers
where c.ContactName.Contains("ann")
select new { c.CustomerID, c.ContactName }
)
.Union
(
from c in Customers
where c.ContactName.Contains("von")
select new { c.CustomerID, c.ContactName }
)

Customers
.Where (c => c.ContactName.Contains ("ann"))
.Select ( c => new { c.CustomerID, c.ContactName } )
.Union (
Customers
.Where (c => c.ContactName.Contains ("von"))
.Select ( c => new { c.CustomerID, c.ContactName } )
)

SELECT [t0].[CustomerID], [t0].[ContactName] FROM [Customers] AS [t0] WHERE [t0].[ContactName] LIKE '%ann%'
UNION
SELECT [t0].[CustomerID], [t0].[ContactName] FROM [Customers] AS [t0] WHERE [t0].[ContactName] LIKE '%von%'

Image may be NSFW.
Clik here to view.

Intersect

產生兩個序列的交集(∩)

Image may be NSFW.
Clik here to view.


(
from c in Customers
where c.ContactName.Contains("ann")
select new { c.CustomerID, c.ContactName }
)
.Intersect
(
from c in Customers
where c.ContactName.Contains("von")
select new { c.CustomerID, c.ContactName }
)

Customers
.Where (c => c.ContactName.Contains ("ann"))
.Select ( c => new { c.CustomerID, c.ContactName } )
.Intersect (
Customers
.Where (c => c.ContactName.Contains ("von"))
.Select ( c => new { c.CustomerID, c.ContactName } )
)

SELECT DISTINCT [t0].[CustomerID], [t0].[ContactName]
FROM [Customers] AS [t0]
WHERE ([t0].[ContactName] LIKE '%ann%')
And
EXISTS(
SELECT NULL FROM [Customers] AS [t1]
WHERE ([t0].[CustomerID] = [t1].[CustomerID]) And ([t1].[ContactName] LIKE '%von%')
)

Image may be NSFW.
Clik here to view.

Except

產生兩個序列的差集

Image may be NSFW.
Clik here to view.


(
from c in Customers
where c.ContactName.Contains("ann")
select new { c.CustomerID, c.ContactName }
)
.Except
(
from c in Customers
where c.ContactName.Contains("von")
select new { c.CustomerID, c.ContactName }
)

Customers
.Where (c => c.ContactName.Contains ("ann"))
.Select ( c => new { c.CustomerID, c.ContactName } )
.Except (
Customers
.Where (c => c.ContactName.Contains ("von"))
.Select ( c => new { c.CustomerID, c.ContactName } )
)

SELECT DISTINCT [t0].[CustomerID], [t0].[ContactName]
FROM [Customers] AS [t0]
WHERE ([t0].[ContactName] LIKE '%ann%')
And
NOT EXISTS(
SELECT NULL FROM [Customers] AS [t1]
WHERE ([t0].[CustomerID] = [t1].[CustomerID]) And ([t1].[ContactName] LIKE '%von%')
)

Image may be NSFW.
Clik here to view.


Viewing latest article 20
Browse Latest Browse All 41

Trending Articles