假设,我有一个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)))
答案 0 :(得分:7)
好吧,我可能会使用查询表达式:
var query = from tuple in enumerable
from replacement in ReplacementFunction(tuple.Item1)
select Tuple.Create(replacement, tuple.Item2);
但那基本上同样的事情......