简体   繁体   中英

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:

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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