2009-10-08 33 views
18

Tôi có một bảng sản phẩm bán hàng trông như thế này:Nhiều nhóm bằng và Sum LINQ

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

tôi cần phải làm cho SUM của mỗi MONTH cho bảng cuối cùng sẽ trông như thế này:

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

Tôi có thể làm điều này với LINQ không?

Trả lời

44
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 } 
+0

Wow đó là cách tắt hiểu biết của tôi nếu LINQ .... Tôi sẽ cố gắng này sau đó trả lời .. cảm ơn – Luiscencio

+0

Nếu bạn đang chạy điều này đối với SQL cho tôi biết làm thế nào nó đi. –

+0

nó hoạt động khá tốt, chỉ cần sửa đổi một số chi tiết, tôi đang sử dụng nó chống lại một đối tượng datatabel, tôi sẽ thử lại sau SQL ... thans = 3 – Luiscencio

2

Chắc chắn.

Nó sẽ giống như thế này:

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

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

Ngoài ra, lưu ý rằng tôi đã hoạt động theo giả định rằng dữ liệu của bạn chỉ có dữ liệu năm 2009. Nếu bạn muốn nhóm theo tháng/năm, bạn có thể sử dụng saleDate.ToString ("yyyyMM") thay vì saleDate.Month.

+0

Bạn cũng có thể sử dụng (saleDate.Year * 12 + saleDate.Month) –

4
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; } 
}