2010-01-04 59 views
5

根據我對的瞭解,其中LINQ中的子句中,它將基於每個元素的所有可能組合的兩個或多個元素的元素進行組合,然後應用這些條件。例如:可能在LINQ中做一個聯合?

public static void Main(string[] args) 
{ 
    var setA = new[] {3, 4, 5}; 
    var setB = new[] {6, 7, 8}; 

    var result = from a in setA 
       from b in setB 
       let sum = a + b 
       where sum == 10   // Where, criteria sum == 10. 
       select new {a, b, sum}; 

    foreach (var element in result) 
     Console.WriteLine("({0},{1}) == {2}", element.a, element.b, element.sum); 

    Console.ReadLine(); 
} 

這產生其中標準施加之前以下結果。

3,6 = 9 4,6 = 10 5,6 = 11 
3,7 = 10 4,7 = 11 5,7 = 12 
3,8 = 11 4,8 = 12 5,8 = 13 

與條件匹配的結果是3,7和4,6。這將產生的結果:

(3,7) == 10 
(4,6) == 10 

但是,從我記得上小學的集理論,有沒有提供工會兩套(僞)的方式:

{3,4 ,5}工會{6,7,8} = {3,4,5,6,7,8}

謝謝,

斯科特

回答

10
var Result = setA.Union(setB) 

有關所有運營商的完整列表,請參閱Enumerable

+0

var result = ( from a in setA from b in setB let sum = a + b where sum == 10 select new {a, b, sum} ).Take(2).ToList(); 

評價所得的實際順序。哈哈,我肯定覺得啞巴。我什麼時候想使用哪裏,生成所有可能的組合? – 2010-01-04 21:42:04

3

Where不會產生所有可能的配對.... Where過濾器。

SelectMany生產的所有可能的配對:

var pairings = SetA 
    .SelectMany(a => SetB, (a, b) => new {a, b}); 

var pairings = 
    from a in SetA 
    from b in SetB 
    select new {a, b}; 

由於延遲執行,LINQ到對象查詢claues以不同的順序可能比你懷疑進行評估。回想一下:查詢在被枚舉之前不會被評估(例如 - 在foreach循環或者.ToList()方法調用中使用)。考慮一對夫婦額外的方法調用原始查詢:以這種方式

* ToList calls its inner enumerable 
    * Take(2) calls its inner enumerable 
    * Select calls its inner enumerable 
     * Where calls its inner enumerable 
     * from (setb) calls its inner enumerable 
      * from (seta) returns 3 from seta 
     * from (setb) returns a pairing of 3 and 6 
     * Where filters out the pairing and calls its inner enumerable for another one 
     * from (setb) returns a pairing of 3 and 7 
     * Where checks the pairing and returns it 
    * Select uses the pairing to create the anonymous instance. 
    *Take(2) returns the anonymous instance and remembers that it has returned once. 
*ToList adds the element to a List 
*ToList calls its inner enumerable 
    * Take(2) calls its inner enumerable 
    * Select calls its inner enumerable 
     * Where calls its inner enumerable 
     * from (setb) returns a pairing of 3 and 8 
     * Where filters out the pairing and calls its inner enumerable for another one 
     * from (setb) calls its inner enumerable 
      * from (seta) returns 4 from seta 
     * from (setb) returns a pairing of 4 and 6 
     * Where checks the pairing and returns it 
    * Select uses the pairing to create the anonymous instance. 
    *Take(2) returns the anonymous instance and remembers that it has returned twice. 
*ToList adds the instance to a List. 
*ToList askes Take(2) if there's any more, and Take(2) say no. 
*ToList returns the list. 
+0

它會過濾,但在過濾之前會創建配對,不是嗎? – 2010-01-06 00:47:46

+0

創建配對 - 在'where'過濾它們之前。 '哪裏'與配對創作無關。讓我們具體一點,不要用「it」這個詞。 – 2010-01-06 15:23:25

+0

啊,對不起。好吧,事件的順序是:1.配對,2.基於哪裏過濾? – 2010-01-07 01:44:46

2
new []{1,2,3}.Concat(new [] {4,5,6}); 
+0

這是有創意的;我沒有考慮過。 – 2010-01-06 00:48:26