0

下面是 C# 代码。

问题是计算出来的SumCustomerDebt

我需要根据 分组CustomerLocation,但CustomerDebt在表中重复多次(针对每个客户)。我需要计算SumCustomerDebt- 作为CustomerDebt根据 distinct的总和CustomerID。我该怎么做?

protected DataTable generateData()
{
    DataTable dt = new DataTable();
    DataColumn column1 = new DataColumn("salesId", Type.GetType("System.Int32"));
    dt.Columns.Add(column1);
    DataColumn column2 = new DataColumn("CustomerLocation", Type.GetType("System.String"));
    dt.Columns.Add(column2);
    DataColumn column3 = new DataColumn("CustomerID", Type.GetType("System.Int32"));
    dt.Columns.Add(column3);
    DataColumn column4 = new DataColumn("CustomerDebt", Type.GetType("System.Int32"));
    dt.Columns.Add(column4);
    DataColumn column5 = new DataColumn("SubTotal", Type.GetType("System.Int32"));
    dt.Columns.Add(column5);


    DataRow dr = dt.NewRow();
    dr["salesId"] = 1;
    dr["CustomerLocation"] = "Chichago";
    dr["CustomerID"] = 1;
    dr["CustomerDebt"] = 100;
    dr["SubTotal"] = 10;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["salesId"] = 2;
    dr["CustomerLocation"] = "Chichago";
    dr["CustomerID"] = 1;
    dr["CustomerDebt"] = 100;
    dr["SubTotal"] = 40;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["salesId"] = 3;
    dr["CustomerLocation"] = "Chichago";
    dr["CustomerID"] = 2;
    dr["CustomerDebt"] = 50;
    dr["SubTotal"] = 30;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["salesId"] = 4;
    dr["CustomerLocation"] = "Miami";
    dr["CustomerID"] = 3;
    dr["CustomerDebt"] = 20;
    dr["SubTotal"] = 50;
    dt.Rows.Add(dr);

    return dt;

}

protected void test()
{
    DataTable dt = generateData();

    var result = from row in dt.AsEnumerable()
                  group row by new
                  {
                      CustomerLocation = row.Field<string>("CustomerLocation"),
                  } into grp
                  select new
                  {
                      CustomerLocation = grp.Key.CustomerLocation,
                      SumSubTotal = grp.Sum(r => r.Field<int>("SubTotal")),
                      OrderCount = grp.Count(),
                      SumCustomerDebt = grp.Sum(r => r.Field<int>("CustomerDebt")),  //this is incorrect, how can i calculate it?
                  };


    foreach (var item in result)
    {
        string info = string.Format("CustomerLocation={0},SumSubTotal={1},OrderCount={2},SumCustomerDebt={3}", item.CustomerLocation, item.SumSubTotal, item.OrderCount, item.SumCustomerDebt);
        Console.WriteLine(info);

    }

}
4

2 回答 2

1

你可以试试这个:

    var Group = dt.AsEnumerable().GroupBy(x => x["CustomerLocation"].ToString()).ToDictionary(x => x.Key, x => x.ToList());

    foreach (var Items in Group)
    {
        foreach (var DistinctCustomerID in Items.Value.Select(x => x["CustomerID"].ToString()).Distinct())
{
            // Gets first instance only
            var Instance = dt.AsEnumerable().Where(x => x[DistinctCustomerID] == DistinctCustomerID).FirstOrDefault();

            int SumCustomerDebt = Convert.ToInt32(Instance["CustomerDebt"].ToString());

            var Customer = new { CustomerLocation = Items.Key, SumSubTotal = Instance["SumSubTotal"].ToString(), OrderCount = Instance["OrderCount"].ToString(), SumCustomerDebt = SumCustomerDebt };

            Console.WriteLine("You can Print Customer object's Properties");
        }
    }
于 2013-11-06T08:23:54.960 回答
0

按客户 ID 对位置行进行分组,并从每个组中选择第一个客户的债务:

var query = from r in generateData().AsEnumerable()
            group r by r.Field<string>("CustomerLocation") into g
            select new
            {
                CustomerLocation = g.Key,
                SumSubTotal = g.Sum(r => r.Field<int>("SubTotal")),
                OrderCount = g.Count(),
                SumCustomerDebt = 
                      g.GroupBy(r => r.Field<int>("CustomerID"))
                       .Sum(cg => cg.First().Field<int>("CustomerDebt"))
            };

这会产生您预期的结果:

{ CustomerLocation = Chichago, SumSubTotal = 80, OrderCount = 3, SumCustomerDebt = 150 } { CustomerLocation = Miami, SumSubTotal = 50, OrderCount = 1, SumCustomerDebt = 20 }

于 2013-11-06T12:59:16.793 回答