开发者

Sorting, Customizing and Generating with Raw Data or Objects

I have a list of Customers records , may be in thousands ,

I want to generate reports (Crystal or MS ) like in hierarchy.

it should be something like this

Customer By country , then Customer by City with in a Country , Then Customers in Areas , and then Male and Female in those area.

I also want to show customer plusminus Calculated from Top .

Like 4 Customer like in NYC and all have +500 , so I have value in US 2000;

any Idea , Hint algorithm how I can achieve this?

here is the Customer Object and example Customers .

public class Customer
{
    public int CutIND { get; set; }
    pu开发者_开发技巧blic 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; }
}

and example customers

        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;


You can use LINQ queries to relatively easily group data hierarchically at multiple levels.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜