开发者

Is it possible to Merge stacks with LINQ

开发者 https://www.devze.com 2023-01-09 00:30 出处:网络
I\'m just starting to learn LINQ, I\'m wondering if it would b开发者_高级运维e possible to group the elements in 3 different stacks using LINQ.

I'm just starting to learn LINQ, I'm wondering if it would b开发者_高级运维e 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;


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


var uniqueValues = stack1
    .Concat(stack2)
    .Concat(stack3)
    .Distinct();

or you could use Union:

var uniqueValues = stack1
    .Union(stack2)
    .Union(stack3);


This is untested:

var uniqueValues =
    from stack in stacks
    from n in stack
    group n by n into nGroup
    select nGroup.Key;
0

精彩评论

暂无评论...
验证码 换一张
取 消