如何在迭代它时将列表拆分为两个并使用LINQ将结果放在元组中?(How to split a list in two while iterating over it and place the results in a tuple using LINQ?)
如果我有一个可枚举的话是否可能
Input :[1,2,3,4] Output :([1,3],[2,4])迭代一次并开始将奇数放在序列/列表/中,甚至在另一个中,最后将两个序列放在一个元组中?
我尝试过类似的东西:
class Elem { int x; } (rez1,rez2)=from value in Generator() let odds=Enumerable.Empty<Elem>() let evens=Enumerable.Empty<Elem>() select(value%2==0?odds.**append**(value):odds.**append**(value))我如何指导元组的一个元素的选择或每个元素的另一个元素?我可以在linq查询中有两个可变列表吗?
PS我试图实现类似的东西(类似于Haskell):
select element -> predicate True -> goes to first element of result tuple predicate False-> goes to second element of result tuple一切都在0(n)
Is it possible if i have an enumerable
Input :[1,2,3,4] Output :([1,3],[2,4])to iterate once over it and start putting odd numbers in a sequence/list/whatever and even in another and at the end placing the two sequences in a tuple?
I have tried something like:
class Elem { int x; } (rez1,rez2)=from value in Generator() let odds=Enumerable.Empty<Elem>() let evens=Enumerable.Empty<Elem>() select(value%2==0?odds.**append**(value):odds.**append**(value))How can i direct the select over one element of the tuple or the other for each element?Can i have two mutable lists inside a linq query?
P.S I am trying to achieve something like (similar to Haskell) :
select element -> predicate True -> goes to first element of result tuple predicate False-> goes to second element of result tupleEverything in 0(n)
最满意答案
创建如下的元组。 对奇数和偶数值运行两个查询。
Tuple.Create(input.Where(c => c % 2 == 1).ToArray(), input.Where(c => c % 2 == 0).ToArray());Create tuple like the following. Run two queries for odd and even values.
Tuple.Create(input.Where(c => c % 2 == 1).ToArray(), input.Where(c => c % 2 == 0).ToArray());更多推荐
发布评论