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;
精彩评论