1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); var productList = new List<Product> { new Product {time = new DateTime(2009, 1, 1, 1, 0, 0), place = "A", price = 10}, new Product {time = new DateTime(2009, 1, 1, 1, 10, 0), place = "A", price = 10}, new Product {time = new DateTime(2009, 1, 1, 2, 10, 0), place = "B", price = 10}, new Product {time = new DateTime(2009, 1, 1, 2, 20, 0), place = "C", price = 10}, new Product {time = new DateTime(2009, 1, 3, 3, 1, 1), place = "D", price = 10}, }; var query = from p in productList group p by new { p.time.Date, p.place } into g select new { g.Key.Date, g.Key.place, Total = g.Sum(p => p.price) }; foreach (var q in query) { Console.WriteLine(string.Format("date: {0}, place:{1}, total:{2}", q.Date, q.place, q.Total)); } } } public class Product { public DateTime time { get; set; } public string place { get; set; } public int price { get; set; } } } |
Direct link: https://paste.plurk.com/show/16960