【发布时间】:2019-10-26 00:52:55
【问题描述】:
我正在尝试实现一件非常简单的事情。我有一个 Enumerable 元组,我想同时映射和解构它们(因为使用 .Item1、.Item2 非常丑陋)。
类似这样的:
List<string> stringList = new List<string>() { "one", "two" };
IEnumerable<(string, int)> tupleList =
stringList.Select(str => (str, 23));
// This works fine, but ugly as hell
tupleList.Select(a => a.Item1 + a.Item2.ToString());
// Doesn't work, as the whole tuple is in the `str`, and num is the index
tupleList.Select((str, num) => ...);
// Doesn't even compile
tupleList.Select(((a, b), num) => ...);
【问题讨论】:
-
IEnumerable<(string str, int num)> ...这也可以用作强制转换/新变量,以防您无法控制原始枚举的创建。
标签: c# linq dictionary tuples