【问题标题】:How to group list childrens when parents is same当父母相同时如何分组列出孩子
【发布时间】:2018-11-04 17:17:28
【问题描述】:

我有一个列表Business,看起来像这样(Company:列出父级;Item:列出子级)

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

我想将CompanyTaxRate 组合成这样的样子

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   

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

【问题讨论】:

  • 该“列表”如何存储在您的应用程序中?如果匹配公司的税率和/或月份不同,该怎么办?
  • 我需要您澄清一下如何在应用程序中存储该列表 - 提供您使用的类的详细信息。
  • TaxRate在匹配的公司中没有变化,所以我用它来为按公司分组的条件,Month可以改变
  • 亲爱的 PaulF,我只需要这个问题的算法或解决方案
  • 如果您需要回答您的问题,那么您需要回答我的问题 - 您的数据是如何存储的。它是在数据库中还是在文件中?当您将这些数据读入应用程序时,它是如何存储的,您使用哪些类?所有这些都是为您提供算法或解决方案所必需的。

标签: c# asp.net algorithm entity-framework linq


【解决方案1】:

这是我发现完成任务的最佳方式:

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;}
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 2020-11-10
    • 1970-01-01
    相关资源
    最近更新 更多