// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Linq; namespace E { public class OrderItem { public int ID { get; set; } public int TypeID { get; set; } public int MenuID { get; set; } public string Name { get; set; } public int Price { get; set; } public int Quantity { get; set; } public int LineTotal { get { return Price*Quantity; } } // This is not a real property !!!! } public class Program { public static void Main(string[] args) { List lst = new List { new OrderItem { ID = 1, TypeID = 1, MenuID = 1, Name = "A", Price = 100, Quantity = 2 }, new OrderItem { ID = 2, TypeID = 1, MenuID = 1, Name = "A", Price = 100, Quantity = 1 }, new OrderItem { ID = 3, TypeID = 1, MenuID = 2, Name = "C", Price = 50, Quantity = 1 }, new OrderItem { ID = 4, TypeID = 2, MenuID = 1, Name = "D", Price = 75, Quantity = 1 } }; foreach(var o in lst.GroupBy(oi => Tuple.Create(oi.TypeID, oi.MenuID)).Select(g => Tuple.Create(g.Key, g.Sum(oi => oi.Quantity), g.Sum(oi => oi.LineTotal)))) { Console.WriteLine("{0} {1} {2} {3}", o.Item1.Item1, o.Item1.Item2, o.Item2, o.Item3); } } } }