简体   繁体   English

需要一点帮助的SelectMany

[英]Need a little help with SelectMany

Suppose, I have an IEnumerable<Tuple<string, object>> and that I want to replace for each element of the enumeration the first element by a list of (several) other elements: 假设我有一个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 } }

How can I accomplish this with SelectMany in a better way than SelectMany ,我如何能以更好的方式完成此任务

enumerable.SelectMany(item => ReplacementFunction(item.Item1).Select(newItem =>
new Tuple<string, object>(newItem, item.Item2)))

Well, I'd probably use a query expression instead: 好吧,我可能会改用查询表达式:

var query = from tuple in enumerable
            from replacement in ReplacementFunction(tuple.Item1)
            select Tuple.Create(replacement, tuple.Item2);

But that's basically the same thing... 但这基本上是一样的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM