gpt4 book ai didi

c# - 按属性对对象列表进行分组,分成多个列表。

转载 作者:太空狗 更新时间:2023-10-30 01:29:55 26 4
gpt4 key购买 nike

当前结构(由于系统要求无法更改)

class Statement
{
string section;
string category;
string statement;
}

示例语句列表

section      category     statement
1 a apple
1 a banana
1 b potato
2 c car
2 c bus
2 d plane

问题

我从 List<Statement> 开始, 需要根据Section进行拆分,然后归类为如下(或类似)结构

struct SectionCollection
{
string sectionName {get{return categories[0].section;}}
List<CategoryCollection> categories;
}

struct CategoryCollection
{
string categoryName {get{return statements[0].category;}}
List<Statement> statements;
}

所以,从 List<Statement> ,我应该有一个 List<SectionCollection> , 其中有一个 List<CategoryCollection> , 其中有一个 List<Statement>

所以在上面的数据示例中,我会有

  • List<SectionCollection>
    • List<CategoryCollection> (里面)
      • List<Statement> (内部)

注意事项

A 语句或类别在不同部分中可能相同并非不可能 - 这些仍然需要属于不同的 SectionCollection

尝试

我目前的尝试一直有效,直到最终在内部 for 循环中抛出空异常。这是我已经关注了一段时间的问题之一,所以我知道这个“解决方案”看起来有多困惑。

var sections = statements.GroupBy(x => x.section).Select(y => y.ToList()).ToList();
foreach(var section in sections)
{
SectionCollection thisSection = new SectionCollection();
var categories = section.GroupBy(x => x.category).Select(y => y.ToList()).ToList();

foreach(var category in categories)
{
thisSection.categories.Add(new CategoryCollection({statements = category});
}
}

最佳答案

您收到空引用错误的原因是您没有初始化 SectionCollection 中的 Categories 列表。

改变:

SectionCollection thisSection = new SectionCollection();

收件人:

SectionCollection thisSection = new SectionCollection() 
{
Categories = new List<CategoryCollection>()
};

将修复错误。您也不会在任何地方捕获结果,如果您将代码更新到下面它应该可以工作:

var sections = statements.GroupBy(x => x.Section).Select(y => y.ToList()).ToList();

var result = new List<SectionCollection>();

foreach (var section in sections)
{
SectionCollection thisSection = new SectionCollection() { Categories = new List<CategoryCollection>() };

var categories = section.GroupBy(x => x.Category).Select(y => y.ToList()).ToList();

foreach (var category in categories)
{
thisSection.Categories.Add(new CategoryCollection { Statements = category });
}

result.Add(thisSection);
}

但是为类提供适当的构造函数和属性并将一些逻辑移到那里可能会更简洁:

internal class Program
{
static void Main(string[] args)
{
var statements = new List<Statement>()
{
new Statement(1, "a", "apple"),
new Statement(1, "a", "banana"),
new Statement(1, "b", "potato"),
new Statement(2, "c", "car"),
new Statement(2, "c", "bus"),
new Statement(2, "d", "plane")
};

var sectionCollections = statements
.GroupBy(s => s.Section)
.Select(group => new SectionCollection(group.Key, statements))
.ToList();
}

public class Statement
{
public Statement(int section, string category, string statementName)
{
Section = section;
Category = category;
StatementName = statementName;
}

public int Section { get; }

public string Category { get; }

public string StatementName { get; }
}

public class SectionCollection
{
public SectionCollection(int sectionName, List<Statement> statements)
{
SectionName = sectionName;

Categories = statements
.Where(s => s.Section == sectionName)
.GroupBy(s => s.Category)
.Select(group => new CategoryCollection(group.Key, group.ToList()))
.ToList();
}

public int SectionName { get; }

public List<CategoryCollection> Categories { get; }
}

public class CategoryCollection
{
public CategoryCollection(string categoryName, List<Statement> statements)
{
CategoryName = categoryName;
Statements = statements;
}

public string CategoryName { get; }

public List<Statement> Statements { get; }
}
}

你最终会得到以下结构:

output structure

关于c# - 按属性对对象列表进行分组,分成多个列表。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48917219/

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