C# 父母相同时如何分组列出子女

C# 父母相同时如何分组列出子女,c#,asp.net,algorithm,entity-framework,linq,C#,Asp.net,Algorithm,Entity Framework,Linq,我有一个列表业务,看起来是这样的(公司:列出父项;项目:列出子项) 我想把公司按税率分组,看看这个 Business -------- CompanyA TaxRate Item1 Name Price Total Item2 Name Price Total Item5 Name Price Total Item

我有一个列表
业务
,看起来是这样的(
公司
:列出父项;
项目
:列出子项)

我想把
公司
税率
分组,看看这个

Business
--------
CompanyA
    TaxRate
    Item1
        Name
        Price
        Total
    Item2
        Name
        Price
        Total
    Item5
        Name
        Price
        Total
    Item6
        Name
        Price
        Total
    Month
CompanyB
    TaxRate
    Item3
        Name
        Price
        Total
    Item4
        Name
        Price
        Total
    Month   

我希望有人能在这种情况下帮助我

这是我找到的完成任务的最佳方式:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;
using System.Data;

namespace ConsoleApplication45
{
    class Program
    {

        static void Main(string[] args)
        {
            List<Business> companies = new List<Business>() {
                new Business() { name = "CompanyA",items = new List<Item>(){ new Item() { name = "Item1"}, new Item() { name = "Item2"}}},
                new Business() { name = "CompanyB", items = new List<Item>() { new Item() { name = "Item3"}, new Item() { name = "Item4"}}},
                new Business() { name = "CompanyA",items = new List<Item>(){ new Item() { name = "Item5"}, new Item() { name = "Item6"}}},
            };


            var groups = companies.GroupBy(x => x.name).Select(x => new { business = x.FirstOrDefault(), items = x.SelectMany(y => y.items) });
            List<Business> newCompanies = new List<Business>();
            foreach (var group in groups)
            {
                Business company = group.business;
                company.items = group.items.ToList();
                newCompanies.Add(company);
            }
        }

    }
    public class Business
    {
        public string name { get; set; }
        public List<Item> items { get; set; }
        public decimal taxrate { get; set; }
        public string month { get; set; }
    }
    public class Item
    {
        public string name { get;set;}
        public decimal price { get;set;}
        public decimal total { get;set;}
    }
}
使用系统;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
使用系统诊断;
使用系统数据;
命名空间控制台应用程序45
{
班级计划
{
静态void Main(字符串[]参数)
{
上市公司=新上市公司(){
新业务(){name=“CompanyA”,items=new List(){new Item(){name=“Item1”},new Item(){name=“Item2”},
新业务(){name=“CompanyB”,items=new List(){new Item(){name=“Item3”},new Item(){name=“Item4”}},
新业务(){name=“CompanyA”,items=new List(){new Item(){name=“Item5”},new Item(){name=“Item6”},
};
var groups=companys.GroupBy(x=>x.name).Select(x=>new{business=x.FirstOrDefault(),items=x.SelectMany(y=>y.items)});
列表新窗格=新列表();
foreach(组中的var组)
{
商业公司=集团商业;
company.items=group.items.ToList();
新增(公司);
}
}
}
公营业务
{
公共字符串名称{get;set;}
公共列表项{get;set;}
公共十进制税率{get;set;}
公共字符串月份{get;set;}
}
公共类项目
{
公共字符串名称{get;set;}
公共十进制价格{get;set;}
公共十进制总数{get;set;}
}
}

该“列表”如何存储在应用程序中?如果匹配公司的税率和/或月份不同,会发生什么情况?我需要您澄清如何在应用程序中存储该列表-提供您使用的类的详细信息。
匹配公司中的税率不会更改,因此我使用它来为每个公司制定条件,
月份
可以更改亲爱的PaulF,我只需要这个问题的算法或解决方案如果你需要回答你的问题,那么你需要回答我的问题-你的数据是如何存储的。它是在数据库中还是在文件中?当您将数据读入应用程序时,它是如何存储的,您使用什么类?所有这些都需要给你一个算法或解决方案。
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;
using System.Data;

namespace ConsoleApplication45
{
    class Program
    {

        static void Main(string[] args)
        {
            List<Business> companies = new List<Business>() {
                new Business() { name = "CompanyA",items = new List<Item>(){ new Item() { name = "Item1"}, new Item() { name = "Item2"}}},
                new Business() { name = "CompanyB", items = new List<Item>() { new Item() { name = "Item3"}, new Item() { name = "Item4"}}},
                new Business() { name = "CompanyA",items = new List<Item>(){ new Item() { name = "Item5"}, new Item() { name = "Item6"}}},
            };


            var groups = companies.GroupBy(x => x.name).Select(x => new { business = x.FirstOrDefault(), items = x.SelectMany(y => y.items) });
            List<Business> newCompanies = new List<Business>();
            foreach (var group in groups)
            {
                Business company = group.business;
                company.items = group.items.ToList();
                newCompanies.Add(company);
            }
        }

    }
    public class Business
    {
        public string name { get; set; }
        public List<Item> items { get; set; }
        public decimal taxrate { get; set; }
        public string month { get; set; }
    }
    public class Item
    {
        public string name { get;set;}
        public decimal price { get;set;}
        public decimal total { get;set;}
    }
}