首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将平面数据收集转换为分层数据收集?

如何将平面数据收集转换为分层数据收集?
EN

Stack Overflow用户
提问于 2015-03-23 22:36:45
回答 1查看 71关注 0票数 1

我有以下收集形式的统一数据:

代码语言:javascript
复制
historyId   documentId   statusId
612         2            1
612         3            2
612         4            5
612         2            4

当上述数据被填充到myClassFlat的胶体中时,它将有四个成员在集合中。

代码语言:javascript
复制
class myClassFlat{
    public int historyId {get; set;}
    public int documentId { get; set;}
    public int statusId {get; set;}
}

我希望将继承数据转换为另一个集合,如下所示,这样,documentId中的重复值就会被满足,并且statusId被加载到类中的数组中:

代码语言:javascript
复制
historyId   documentId   statusId
612         2            1, 4
612         3            2
612         4            5

因此,当数据被填充到以下myClassHierarchical的集合中时,它将只有三个成员,因为statusId的两个值1和4将被填充到类的int[] statusId中。

代码语言:javascript
复制
class myClassHierarchical{
    public int historyId {get; set;}
    public int documentId { get; set;}
    public int[] statusId {get; set;}
}

我首先尝试通过documentId使用Linq查找集合的唯一成员,然后循环非唯一成员,创建类的列表并使用statusId填充int[],但无法使其工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-23 22:50:19

看起来,您只想按两个字段分组:

代码语言:javascript
复制
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()
                     });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29221743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档