2013年4月10日 星期三

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%'

Union

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

(
	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%'

Intersect

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

(
	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%')
)

Except

產生兩個序列的差集

(
	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%')
)

沒有留言:

張貼留言