【问题标题】:Multiple group by and Sum LINQ多个 group by 和 Sum LINQ
【发布时间】:2009-10-09 07:47:54
【问题描述】:

我有一个如下所示的产品销售表:

saleDate     prod        qty
10/22/09     soap        10
09/22/09     pills       05
09/25/09     soap        06
09/25/09     pills       15

我需要计算每个月的总和,所以最终表格如下所示:

saleDate     prod        qty
10/09        soap        10
09/09        soap        06
09/09        pills       20

我可以用 LINQ 做到这一点吗?

【问题讨论】:

    标签: c# linq


    【解决方案1】:
    var products = new[] {
        new {SaleDate = new DateTime(2009,10,22), Product = "Soap", Quantity = 10},
        new {SaleDate = new DateTime(2009,09,22), Product = "Pills", Quantity = 5},
        new {SaleDate = new DateTime(2009,09,25), Product = "Soap", Quantity = 6},
        new {SaleDate = new DateTime(2009,09,25), Product = "Pills", Quantity = 15}
    };
    
    var summary = from p in products
                  let k = new
                  {
                       //try this if you need a date field 
                       //   p.SaleDate.Date.AddDays(-1 *p.SaleDate.Day - 1)
                      Month = p.SaleDate.ToString("MM/yy"),
                      Product = p.Product
                  }
                  group p by k into t
                  select new
                  {
                      Month = t.Key.Month,
                      Product = t.Key.Product,
                      Qty = t.Sum(p => p.Quantity)
                  };
    
    foreach (var item in summary)
        Console.WriteLine(item);
    
    //{ Month = 10/09, Product = Soap, Qty = 10 }
    //{ Month = 09/09, Product = Pills, Qty = 20 }
    //{ Month = 09/09, Product = Soap, Qty = 6 }
    

    【讨论】:

    • 哇,如果 linq.... 我会尝试这个然后回复.. 谢谢
    • 如果你在 SQL 上运行这个,请告诉我它是怎么回事。
    • 它工作得很好,只需要修改一些细节,我正在对数据表对象使用它,稍后我会再试一次 SQL...thans =3
    • 这是一个如此疯狂的 LINQ 语句,我喜欢它!
    • 我之前被选为这个查询,现在转身再次使用它。非常感谢。
    【解决方案2】:
    var q = from s in sales
           group s by new {saleDate = s.saleDate.ToString("MM/yy"), s.prod} into g
           select new { g.Key.saleDate, g.Key.prod, qty = g.Sum(s => s.qty) };
    

    【讨论】:

      【解决方案3】:
      class Program
      {
          static void Main(string[] args)
          {
              var sales = new List<Sale>();
              sales.Add(new Sale() { Product = "soap", saleDate = new DateTime(2009, 10, 22), Quantity = 10});
              sales.Add(new Sale() { Product = "soap", saleDate = new DateTime(2009, 9,22), Quantity = 6});
              sales.Add(new Sale() { Product = "pills", saleDate = new DateTime(2009,9,25), Quantity = 15});
              sales.Add(new Sale() { Product = "pills", saleDate = new DateTime(2009,9,25), Quantity = 5});
      
              var q = from s in sales
                      group s by new { s.Product, s.saleDate.Month, s.saleDate.Year } into g
                      select new {Month = String.Format("{0:MM/yy}", new DateTime(g.Key.Year, g.Key.Month, 1)), product = g.Key.Product, quantity = g.Sum(o=>o.Quantity)};
      
      
          }
      }
      
      class Sale
      {
          public DateTime saleDate { get; set; }
          public int Quantity { get; set; }
          public string Product { get; set; }
      }
      

      【讨论】:

        【解决方案4】:

        当然。

        看起来像这样:

        var GroupedSales = 
              from p in products 
              group p by p.saleDate.Month into g 
              select new { saleMonth = g.saleDate.Month, QtySold = g.Sum(p => p.qty) }; 
        

        见:http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx#sumGrouped

        另外,请注意,我是在假设您的数据集只有 2009 年的数据的情况下进行操作的。如果要按月/年分组,可以使用 saleDate.ToString("yyyyMM") 代替 saleDate.Month。

        【讨论】:

        • 也可以使用(saleDate.Year*12+saleDate.Month)
        猜你喜欢
        • 2021-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-18
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        相关资源
        最近更新 更多