假设,我有一个 IEnumerable<Tuple<string, object>>
并且我想用(几个)其他元素的列表替换枚举的每个元素的第一个元素:
Original enumerable: { { "a", obj1 }, { "b", obj2 } }
First-element replacement: a -> { "c", "d" }, b -> { "e", "f", "g" }
Result : { { "c", obj1 }, { "d", obj1 }, { "e", obj2 }, { "f" , obj2 }, { "g" , obj2 } }
我如何使用 SelectMany
完成此操作?比
enumerable.SelectMany(item => ReplacementFunction(item.Item1).Select(newItem =>
new Tuple<string, object>(newItem, item.Item2)))
最佳答案
好吧,我可能会改用查询表达式:
var query = from tuple in enumerable
from replacement in ReplacementFunction(tuple.Item1)
select Tuple.Create(replacement, tuple.Item2);
但这基本上是同一回事...
关于c# - 在 SelectMany 方面需要一些帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7388348/