我有以下收集形式的统一数据:
historyId documentId statusId
612 2 1
612 3 2
612 4 5
612 2 4当上述数据被填充到myClassFlat的胶体中时,它将有四个成员在集合中。
class myClassFlat{
public int historyId {get; set;}
public int documentId { get; set;}
public int statusId {get; set;}
}我希望将继承数据转换为另一个集合,如下所示,这样,documentId中的重复值就会被满足,并且statusId被加载到类中的数组中:
historyId documentId statusId
612 2 1, 4
612 3 2
612 4 5因此,当数据被填充到以下myClassHierarchical的集合中时,它将只有三个成员,因为statusId的两个值1和4将被填充到类的int[] statusId中。
class myClassHierarchical{
public int historyId {get; set;}
public int documentId { get; set;}
public int[] statusId {get; set;}
}我首先尝试通过documentId使用Linq查找集合的唯一成员,然后循环非唯一成员,创建类的列表并使用statusId填充int[],但无法使其工作。
发布于 2015-03-23 22:50:19
看起来,您只想按两个字段分组:
var list = new List<myClassFlat>
{
new myClassFlat() { historyId = 612, documentId = 2, statusId = 1},
new myClassFlat() { historyId = 612, documentId = 3, statusId = 2},
new myClassFlat() { historyId = 612, documentId = 4, statusId = 5},
new myClassFlat() { historyId = 612, documentId = 2, statusId = 4},
};
var result = list.GroupBy(x => new { x.historyId, x.documentId })
.Select(g => new myClassHierarchical()
{
historyId = g.Key.historyId,
documentId = g.Key.documentId,
statusId = g.Select(x => x.statusId).ToArray()
});https://stackoverflow.com/questions/29221743
复制相似问题