【发布时间】:2020-01-23 01:41:00
【问题描述】:
给定一个List<ComplexObject>,我如何创建一个新的命名元组列表?因此,对于原始列表中的每个 ComplexObject,使用来自 ComplexObject 的某些值创建一个新元组?
public class ComplexObject
{
public string SomeString { get; set; }
public int SomeInt { get; set; }
public bool SomeBool { get; set; }
// More properties that are irrelevant for the purpose of this question...
}
这就是我正在尝试的:
List<ComplexObject> complexObjects = new List<ComplexObject>
{
new ComplexObject { SomeString = "SomeString01", SomeInt = 1, SomeBool = true },
new ComplexObject { SomeString = "SomeString02", SomeInt = 2, SomeBool = false },
new ComplexObject { SomeString = "SomeString03", SomeInt = 3, SomeBool = true },
};
List<(string SomeString, int SomeInt, bool SomeBool)> complexTuples
= complexObjects.SelectMany(obj => (obj.SomeString, obj.SomeInt, obj.SomeBool));
但是这会导致错误:
无法从用法中推断方法“Enumerable.SelectMany(IEnumerable, Func>)”的类型参数。尝试明确指定类型参数。
【问题讨论】: