排序,自定义和生成原始数据或对象

本文关键字:原始数据 对象 自定义 排序 | 更新日期: 2023-09-27 18:08:50

我有一个客户记录列表,可能有几千条,

我想生成报告(水晶或MS)像在层次结构。

应该是这样的

按国家划分客户,然后按城市划分客户,然后按地区划分客户,然后按地区划分男性和女性。

我还想显示从顶部计算的客户plus。

像纽约的4个客户,都有+500,所以我有2000美元的价值;

任何想法,提示算法我如何才能实现这一点?

这里是客户对象和示例客户。

public class Customer
{
    public int CutIND { get; set; }
    public string CustName { get; set; }
    public string Country { get; set; }
    public string City { get; set; }
    public string Area { get; set; }
    public string Gender { get; set; }
    public int plusMinus { get; set; }
}

和客户示例

        Customer c1 = new Customer();
        c1.CutIND = 123445;
        c1.CustName = "Sajjad";
        c1.Country = "US";
        c1.City = "NYC";
        c1.Area = "BLueArea";
        c1.plusMinus = -560;

        Customer c2 = new Customer();
        c2.CutIND = 43432;
        c2.CustName = "Mike";
        c2.Country = "UK";
        c2.City = "London";
        c2.Area = "SomeArea";
        c2.plusMinus = 9000;

排序,自定义和生成原始数据或对象

您可以使用LINQ查询相对容易地在多个层次上对数据进行分层分组。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class Customer
  {
     public int CutIND { get; set; }
     public string CustName { get; set; }
     public string Country { get; set; }
     public string City { get; set; }
     public string Area { get; set; }
     public string Gender { get; set; }
     public int plusMinus { get; set; }
     public Customer(int CutIND, string CustName, string Country, string City, string Area, string Gender, int plusMinus)
     {
        this.CutIND = CutIND;
        this.CustName = CustName;
        this.Country = Country;
        this.City = City;
        this.Area = Area;
        this.Gender = Gender;
        this.plusMinus = plusMinus;
     }
  }

  class Program
  {
     static void Main(string[] args)
     {
        Customer[] customers = new Customer[] {
           new Customer(123445, "Sajjad", "US", "NYC", "BLueArea", "M", -560),
           new Customer(43432, "Mike", "UK", "London", "someArea", "M", 9000),
           new Customer(20001, "Mathilde", "OS", "Vienna", "WienerWald", "F", 8192),
           new Customer(20002, "Harry", "US", "NYC", "Broooklyn", "M", 50),
           new Customer(20003, "Jim", "OS", "Vienna", "AIS", "M", 12000),
           new Customer(20004, "Bill", "US", "MSP", "Excelsior", "M", 90)
        };
        var CityGroups =
           from c in customers
           group c by new { Country = c.Country, City = c.City } into cities
           select new { Country = cities.Key.Country, City = cities.Key.City, Total = cities.Sum(c => c.plusMinus), Residents = cities };
        var CountryGroups =
           from city in CityGroups
           group city by city.Country into countries
           select new { Country = countries.Key, Cities = countries, Total = countries.Sum(c => c.Total) };
        foreach (var country in CountryGroups)
        {
           Console.WriteLine("{0} (Total = {1})", country.Country, country.Total);
           foreach (var city in country.Cities)
           {
              Console.WriteLine("  {0} (Total = {1})", city.City, city.Total);
              foreach (var r in city.Residents)
              {
                 Console.WriteLine("    {0} {1} {2} {3}", r.Area, r.CustName, r.Gender, r.plusMinus);
              }
           }
        }
     }
  }
}