很抱歉这个可能很愚蠢的问题,但我有这个 Ienumerable<>
如此设置
我需要将名称和百分比分成单独的行,并为这些行复制 ProductId 和 RowIndex(我知道这效率不高,但这是需要完成的)。并且可能是一个新字段,用于指定组合行具有哪些数据。
例如,
ProductId, Name , Percent, RowIndex
2301283 , PLACEHOLDER, 12.20 , 1
应该变成这样:
ProductId, DataType, Value , RowIndex
2301283 , Name , PLACEHOLDER, 1
2301283 , Percent , 12.20 , 1
etc etc
此外,如果有意义的话,它们不能嵌套在其他列表或枚举或任何东西中。
这在 LINQ 中可能吗?
最佳答案
据我所知,您希望将每个 GI
对象转换为 2 个新对象,并将所有这些对展平为一个可枚举对象。希望我理解正确。
我认为SelectMany
可以胜任这项工作。
yourEnumerable.SelectMany(x => new[] {
new { ProductId = x.ProductId, DataType = "Name", Value = x.Name, RowIndex = x.RowIndex },
new { ProductId = x.ProductId, DataType = "Percent", Value = x.Percent, RowIndex = x.RowIndex }
})
关于C# LINQ - 将行拆分为两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50266496/