将订单行汇总到订单汇总中 - 如何获取数量

时间:2013-08-19 14:33:15

标签: c# linq

我有以下扁平结构:

Name1 | Name2 | Price | SubName | SubPrice
------+-------+-------+---------+---------
A     | one   | 10    | X       |  5
A     | one   | 10    | Y       |  7
A     | one   | 10    | Z       | 11
A     | one   | 10    | X       |  5
A     | one   | 10    | Y       |  7
A     | one   | 10    | Z       | 11
A     | two   | 16    | X       |  5
A     | null  |  9    | null    | null
B     | three | 24    | null    | null

需要将其转换为以下内容:

{
  0 = {
    Name       = "A one X, Y, Z",
    Quantity   = 2,
    TotalPrice = 66,
  },
  1 = {
    Name       = "A two X",
    Quantity   = 1,
    TotalPrice = 21,
  },
  2 = {
    Name       = "A",
    Quantity   = 1,
    TotalPrice = 9,
  },
  3 = {
    Name       = "B three",
    Quantity   = 1,
    TotalPrice = 24,
  }
}

理论很简单 - .GroupBy() Name1和Name2 - 问题是,一旦我.Select()离开小组,我就永远无法获得正确的数量......我知道这应该很简单,但我似乎无法理解它......

items
  .GroupBy(item => new
           {
             item.Name1,
             item.Name2,
           })
  .Select(grouping => new
          {
            Name  = grouping.Key.Name1 + " " + grouping.Key.Name2,
            Price = grouping.Key.Price,
            Subs  = grouping.GroupBy(groupItem => new
                                     {
                                       groupItem.SubName,
                                       groupItem.SubPrice,
                                     }),
          })
  .Select(temp => new
          {
            Name = temp.Name + (temp.Subs.Any() ? " " + temp.Subs.Select(sub => sub.SubName).Aggregate((a, b) => (a + ", " + b)) : string.Empty),
            FullItemPrice = temp.Price + temp.Subs.Sum(subPrice => subPrice ?? 0m),
            Quantity = ???,
          })
  .Select(output => new
          {
            output.Name,
            output.Quantity,
            TotalPrice = output.FullItemPrice * output.Quantity,
          });

1 个答案:

答案 0 :(得分:0)

var source = new List<Item>(9) {
    new Item { Name1 = "A", Name2 = "One", Price = 10, SubName = "X", SubPrice = 5},
    new Item { Name1 = "A", Name2 = "One", Price = 10, SubName = "Y", SubPrice = 7},
    new Item { Name1 = "A", Name2 = "One", Price = 10, SubName = "Z", SubPrice = 11},
    new Item { Name1 = "A", Name2 = "One", Price = 10, SubName = "X", SubPrice = 5},
    new Item { Name1 = "A", Name2 = "One", Price = 10, SubName = "Y", SubPrice = 7},
    new Item { Name1 = "A", Name2 = "One", Price = 10, SubName = "Z", SubPrice = 11},
    new Item { Name1 = "A", Name2 = "Two", Price = 16, SubName = "X", SubPrice = 5},
    new Item { Name1 = "A", Name2 = null, Price = 9, SubName = null, SubPrice = 0 },
    new Item { Name1 = "B", Name2 = "three", Price = 24, SubName = null, SubPrice = 0}
};

var grouped = source.GroupBy(x => new { x.Name1, x.Name2 })
                    .Select(g => new
                    {
                        Name = string.Format("{0} {1} {2}", g.Key.Name1, g.Key.Name2, string.Join(", ", g.Select(x => x.SubName))).Trim(),
                        Quantity = g.Count() / g.Select(x => x.SubName).Distinct().Count()
                    })
                    .ToList();

但是你仍然需要实现Price计算逻辑。