gpt4 book ai didi

C# 将嵌套组转换为 List

转载 作者:行者123 更新时间:2023-11-30 20:36:23 25 4
gpt4 key购买 nike

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

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 的列表等等等等

最佳答案

实际上,用于生成所需结果的 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();

关于C# 将嵌套组转换为 List<MyObject>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37024415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com