有一个简单的模型:
public class SimpleClass
{
public string prop1 { get; set; }
public string prop2 { get; set;}
}
还有一个集合:
List<SimpleClass> myCollection = new List<SimpleClass>();
我想做以下事情:
var headerCollection = new List<string>();
foreach(var item in myCollection)
{
headerCollection.add(item.prop1);
headerCollection.add("Total");
headerCollection.add("Date");
//...
headerCollection.add(item.prop2);
}
但我需要对对象使用 linq。
我试过:
var modifiedHeaders = myCollection
.Select(item => new[] { item.CON_DESCRIPCION, "Total", "Date", item.prop2 });
但它生成了一个类型的集合:List<string[]> .如何解决?
请您参考如下方法:
你只需要使用 SelectMany 而不是 Select:
var mh = myCollection.SelectMany(i =>
new[] { i.CON_DESCRIPTION, "Total", "Date", i.prop2 });
