2010-01-04 21 views
5

Từ những gì tôi hiểu về mệnh đề nơi trong LINQ, nó kết hợp các yếu tố từ hai hay nhiều bộ dựa trên tất cả kết hợp có thể có của mỗi yếu tố và sau đó áp dụng các tiêu chuẩn. Ví dụ:Có thể làm công đoàn trong LINQ không?

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(); 
} 

này tạo ra các kết quả sau đây trước khi nơi tiêu chuẩn được áp dụng.

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 

Kết quả phù hợp với tiêu chí là 3,7 và 4,6. Điều này tạo ra kết quả:

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

Tuy nhiên, từ những gì tôi nhớ trong lý thuyết tập hợp trường lớp, là có một cách để cung cấp các đoàn của hai bộ (giả):

{3, 4 , 5} {đoàn 6, 7, 8} = {3, 4, 5, 6, 7, 8}

Cảm ơn,

Scott

Trả lời

10
var Result = setA.Union(setB) 

Để biết danh sách đầy đủ của tất cả các toán tử, hãy xem Enumerable.

+0

Oh. Ha ha, tôi chắc chắn cảm thấy câm. Khi nào tôi muốn sử dụng ở đâu, tạo ra tất cả các kết hợp có thể? –

3

Where không sản xuất tất cả các cặp có thể .... Where bộ lọc.

SelectMany sản xuất tất cả các cặp có thể:

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

hoặc

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

Do thực hiện chậm, LINQ to phản đối claues truy vấn được đánh giá theo một thứ tự khác nhau hơn bạn có thể nghi ngờ. Nhớ lại: truy vấn không được đánh giá cho đến khi chúng được liệt kê (chẳng hạn như - được sử dụng trong vòng lặp foreach hoặc cuộc gọi phương thức .ToList()). Hãy xem xét các truy vấn ban đầu với một vài thêm lời gọi phương thức:

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(); 

Trình tự thực tế số tiền thu được đánh giá theo cách này

* 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

Nó lọc, nhưng trước khi nó lọc nó tạo ra các cặp, phải không? –

+0

các cặp được tạo - trước khi 'ở đâu' lọc chúng. 'where' không liên quan gì đến việc tạo ghép nối. chúng ta hãy cụ thể và không sử dụng từ "nó". –

+0

Ah, xin lỗi. Ok, do đó, thứ tự của các sự kiện sẽ là: 1. Ghép nối, 2. Bộ lọc dựa trên vị trí? –

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

Đó là sáng tạo; Tôi đã không xem xét điều đó. –

Các vấn đề liên quan