如何将平面数据集合转换为分层数据集合?
How to convert a flat data collection into hierarchical one?
我有一个采集形式的平面数据如下:
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 加载到 class:
中的数组中
historyId documentId statusId
612 2 1, 4
612 3 2
612 4 5
所以当数据被填充到下面的myClassHierarchical的集合中时,它将只有三个成员,因为statusId的两个值,1和4,将被填充到int[]
的statusId中class
class myClassHierarchical{
public int historyId {get; set;}
public int documentId { get; set;}
public int[] statusId {get; set;}
}
我尝试首先使用 Linq 通过 documentId 找出集合的唯一成员,然后遍历非唯一成员并创建一个 classes 列表并使用 statusId 填充 int[],但是无法正常工作。
您似乎只想按两个字段分组:
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()
});
我有一个采集形式的平面数据如下:
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 加载到 class:
中的数组中historyId documentId statusId
612 2 1, 4
612 3 2
612 4 5
所以当数据被填充到下面的myClassHierarchical的集合中时,它将只有三个成员,因为statusId的两个值,1和4,将被填充到int[]
的statusId中class
class myClassHierarchical{
public int historyId {get; set;}
public int documentId { get; set;}
public int[] statusId {get; set;}
}
我尝试首先使用 Linq 通过 documentId 找出集合的唯一成员,然后遍历非唯一成员并创建一个 classes 列表并使用 statusId 填充 int[],但是无法正常工作。
您似乎只想按两个字段分组:
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()
});