tags:

views:

62

answers:

3

I'm just starting to learn LINQ, I'm wondering if it would be possible to group the elements in 3 different stacks using LINQ. this is what I have, could it be possible to add more than one array in the from clause, and how?

        var uniqueValues =
            from n in valuesStack.ToArray()
            group n by n into nGroup
            select nGroup.Key;
A: 

This is untested:

var uniqueValues =
    from stack in stacks
    from n in stack
    group n by n into nGroup
    select nGroup.Key;
Lasse V. Karlsen
+2  A: 

You can Union the stacks together.

var s1 = new Stack<int>();
var s2 = new Stack<int>();
var s3 = new Stack<int>();
var r = s1.Union(s2.Union(s3)).ToArray();
ChaosPandion
Thanks, this works perfect for what I need! I'll keep trying the LINQ option just to know it :)
david
+3  A: 
var uniqueValues = stack1
    .Concat(stack2)
    .Concat(stack3)
    .Distinct();

or you could use Union:

var uniqueValues = stack1
    .Union(stack2)
    .Union(stack3);
Lee
perfect :) So I get that the use of LINQ is not necessary for this, but could it be done?
david
@david: `Union` is considered a LINQ operator. It is part of the `Enumerable` class in the `System.Linq` namespace.
Robert Harvey
@Robery: sort-of a grey area. "LINQ" is "Language Integrated Query", but this is not using any language query features, just the library.
James Curran
@James - Actually I would consider this black-and-white. Unless you write a query such as `from s in sl select s` you are not using LINQ.
ChaosPandion