C#将嵌套组转换为List <myobject>

时间:2016-05-04 09:59:37

标签: c# linq

所以这里有一堆最终会被分组的类。

public class Component
{
    public string Name { get; set;}

    public int ComponentType { get; set; }

    public float Range { get; set; }
}

public class ComponentGroup
{
    /// <summary>
    /// Type of component in the group
    /// </summary>
    public int Type { get; set; }

    /// <summary>
    /// List of Components
    /// </summary>
    public List<Component> Components { get; set; }

    /// <summary>
    /// Range
    /// </summary>
    public float Range { get; set; }
}

public class ComponentGroupType
{
    public ComponentType ComponentType { get; set; }
    public List<ComponentGroup> ComponentGroups { get; set; }

    public ComponentGroupType()
    {
        ComponentGroups = new List<ComponentGroup>();
    }

    public ComponentGroupType(List<ComponentGroup> componentGroups )
    {
        ComponentGroups = componentGroups;
    }
}

现在假设我有List<Component>这样填充。

Component() { Name = "A", ComponentType = 1, Range = 10000 }
Component() { Name = "B", ComponentType = 1, Range = 10000 }
Component() { Name = "C", ComponentType = 1, Range = 10000 }
Component() { Name = "D", ComponentType = 1, Range = 1000 }
Component() { Name = "E", ComponentType = 1, Range = 1000 }
Component() { Name = "F", ComponentType = 2, Range = 1000000 }
Component() { Name = "G", ComponentType = 2, Range = 1000000 }
Component() { Name = "H", ComponentType = 3, Range = 1000000 }

我想要做的是按ComponentType然后按Range对项目进行分组,这意味着我最终会得到类似的结果。

List<ComponentGroupType>
 |- ComponentGroupType() ComponentType = 1 
    |-- ComponentGroup() Range = 10000
        |--- Component() { Name = "A", ComponentType = 1, Range = 10000 }
        |--- Component() { Name = "B", ComponentType = 1, Range = 10000 }
        |--- Component() { Name = "C", ComponentType = 1, Range = 10000 }
    |-- ComponentGroup() Range = 1000
        |--- Component() { Name = "D", ComponentType = 1, Range = 1000 }
        |--- Component() { Name = "E", ComponentType = 1, Range = 1000 }

|- ComponentGroupType() ComponentType = 2
    |-- ComponentGroup() Range = 1000000
        |--- Component() { Name = "F", ComponentType = 2, Range = 1000000 }
        |--- Component() { Name = "G", ComponentType = 2, Range = 1000000 }

|- ComponentGroupType() ComponentType = 3
        |-- ComponentGroup() Range = 1000000
        |--- Component() { Name = "H", ComponentType = 3, Range = 1000000 }

很抱歉代表性差,但希望你知道它们是如何分组的。

所以这是我的嵌套分组代码,似乎可以进行分组部分。

var groups = from component in _components
             group component by component.ComponentType into group1
             from group2 in 
                 (from component in group1
                 group component by component.Range)
             group group2 by group1.Key;

我想要的是将分组数据转换为上述类型,即groupsTypeOf(List<ComponentGroupType>),其中包含ComponentGroups的列表,其中包含Component等列表。

1 个答案:

答案 0 :(得分:4)

实际上,用于生成所需结果的LINQ查询比嵌套分组示例更自然。只需按照您的意愿行事 - 按ComponentType分组,然后按Range分组:

var groups = (
    from component in _components
    group component by component.ComponentType into typeGroup
    select new ComponentGroupType
    {
        ComponentType = typeGroup.Key,
        ComponentGroups = (
           from component in typeGroup
           group component by component.Range into rangeGroup
           select new ComponentGroup
           {
               Type = typeGroup.Key,
               Range = rangeGroup.Key,
               Components = rangeGroup.ToList()
           }
       ).ToList()
    }
).ToList();