c# - 在 SelectMany 方面需要一些帮助

标签 c# linq

假设,我有一个 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/

相关文章:

c# - 如何以及何时为 WinForms 控件设置字体

c# - 谁能解释一下以下两个查询的区别?

c# - LINQ 表达式 - 相同值不超过两次

c# - Linq 找到长度最长的点对?

c# - 使用 C# 和 Fiddler 了解 HTTPS?

c# - Shelltile 在 Windows Phone 7.1 中未正确更新

c# - 如何按类型排序列表?

c# - 两个字符串数组的完全外连接

c# - 如何编写支持对象初始值设定项的自定义 DynamicObject 类

c# - 无法使用 C# 和 SSH.NET 从 Windows 运行 Unix 服务器中的用户脚本