Scott Davies
Scott Davies

Reputation: 3755

Possible to do a union in LINQ?

From what I understand about the where clause in LINQ, it combines elements from two or more sets based on all possible combinations of each element and then applies the criteria. For example:

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

This produces the following results before the where criteria is applied.

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

The results that match the criteria are 3,7 and 4,6. This produces the results:

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

However, from what I remember in grade school set theory, is there a way to provide the union of the two sets (pseudocode):

{3, 4, 5} union {6, 7, 8} = {3, 4, 5, 6, 7, 8}

Thanks,

Scott

Upvotes: 5

Views: 1633

Answers (4)

Amy B
Amy B

Reputation: 110091

Where doesn't produce all possible pairings.... Where filters.

SelectMany produces all possible pairings:

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

or

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

Due to deferred execution, linq to object query claues are evaluated in a different order than you might suspect. Recall: queries are not evaluated until they are enumerated (such as - used in a foreach loop or a .ToList() method call). Consider the original query with a couple extra method calls:

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

The actual order of evaluation proceeds in this fashion

* 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.

Upvotes: 3

George Polevoy
George Polevoy

Reputation: 7671

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

Upvotes: 2

Hamish Grubijan
Hamish Grubijan

Reputation: 10820

A Union is only one of 101 samples listed here:

http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Upvotes: 1

Chris Pitman
Chris Pitman

Reputation: 13104

var Result = setA.Union(setB)

For a full listing of all the operators, look at Enumerable.

Upvotes: 10

Related Questions