fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class MyData
  6. {
  7. public int Id { get; set; }
  8. public string Name { get; set; }
  9. public int Floor { get; set; }
  10. }
  11.  
  12. public class MyGroupedData
  13. {
  14. public string Name { get; set; }
  15. public IEnumerable<int> Floors { get; set; }
  16. }
  17.  
  18. public class Test
  19. {
  20. public static void Main()
  21. {
  22. MyData[] data = {
  23. new MyData() { Id = 1, Name = "Store1", Floor = 1 },
  24. new MyData() { Id = 2, Name = "Store2", Floor = 1 },
  25. new MyData() { Id = 3, Name = "Store2", Floor = 2 },
  26. new MyData() { Id = 4, Name = "Store2", Floor = 3 },
  27. new MyData() { Id = 5, Name = "Store3", Floor = 2 },
  28. };
  29. Console.WriteLine("1");
  30. var groupedData = from x in data group x by x.Name into grp
  31. select new MyGroupedData() { Name = grp.Key, Floors = grp.Select(y => y.Floor) };
  32. Console.WriteLine("2");
  33. foreach(var g in groupedData)
  34. Console.WriteLine("{0} -> {1}", g.Name, string.Join(", ", g.Floors.Select(x => x.ToString()).ToArray()));
  35. }
  36. }
Success #stdin #stdout 0.05s 34800KB
stdin
Standard input is empty
stdout
1
2
Store1 -> 1
Store2 -> 1, 2, 3
Store3 -> 2