fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TestApp
  6. {
  7.  
  8.  
  9. class Program
  10. {
  11.  
  12. static void Main(string[] args)
  13. {
  14. var items = new[]{ new {ItemCode = 100, SoldQty = 1, Description = "desc1", UOM = "xxx"},
  15. new {ItemCode = 100, SoldQty = 2, Description = "desc1", UOM = "xxx"},
  16. new {ItemCode = 200, SoldQty = 1, Description = "desc2", UOM = "xxx"},
  17. new {ItemCode = 200, SoldQty = 7, Description = "desc2", UOM = "xxx"}
  18. };
  19.  
  20. var resQuery = from i in items
  21. group i by new {i.ItemCode,i.Description, i.UOM}into g
  22. select new
  23. {
  24. ItemCode = g.Key.ItemCode,
  25. TotalSold = g.Sum(p => p.SoldQty),
  26. Description = g.Key.Description,
  27. UOM = g.Key.UOM
  28. /// other properties
  29. };
  30.  
  31. foreach (var res in resQuery)
  32. {
  33. System.Console.WriteLine(res.ToString());
  34. }
  35. }
  36. }
  37. }
Success #stdin #stdout 0.05s 34072KB
stdin
Standard input is empty
stdout
{ ItemCode = 100, TotalSold = 3, Description = desc1, UOM = xxx }
{ ItemCode = 200, TotalSold = 8, Description = desc2, UOM = xxx }