fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class LinqGroupDemo
  6. {
  7. static public void Main(string[] args)
  8. {
  9. var query =
  10. from column in GetSource()
  11. group column by new {column.LocId, column.SecId} into g
  12. orderby g.Key.LocId, g.Key.SecId
  13. select new
  14. {
  15. LocId = g.Key.LocId,
  16. SecId = g.Key.SecId,
  17. Columns = g
  18. };
  19.  
  20. foreach (var key in query)
  21. {
  22. Console.WriteLine("LocId:{0}, SecId:{1}",
  23. key.LocId,
  24. key.SecId);
  25.  
  26. foreach (var column in key.Columns)
  27. {
  28. Console.WriteLine(" StartElevation:{0}, EndElevation:{1}",
  29. column.StartElevation,
  30. column.EndElevation);
  31. }
  32. }
  33. }
  34.  
  35. static private List<Column> GetSource()
  36. {
  37. return new List<Column>
  38. {
  39. new Column { LocId = 1 , SecId = 1, StartElevation = 0, EndElevation = 160 },
  40. new Column { LocId = 1 , SecId = 1, StartElevation = 160, EndElevation = 320 },
  41. new Column { LocId = 1 , SecId = 2, StartElevation = 320, EndElevation = 640 },
  42. new Column { LocId = 2 , SecId = 1, StartElevation = 0, EndElevation = 160 },
  43. new Column { LocId = 2 , SecId = 2, StartElevation = 160, EndElevation = 320 }
  44. };
  45. }
  46. }
  47.  
  48. public class Column
  49. {
  50. public int LocId { get; set; }
  51. public int SecId { get; set; }
  52. public double StartElevation { get; set; }
  53. public double EndElevation { get; set; }
  54. }
  55.  
Success #stdin #stdout 0.07s 34288KB
stdin
Standard input is empty
stdout
LocId:1, SecId:1
  StartElevation:0, EndElevation:160
  StartElevation:160, EndElevation:320
LocId:1, SecId:2
  StartElevation:320, EndElevation:640
LocId:2, SecId:1
  StartElevation:0, EndElevation:160
LocId:2, SecId:2
  StartElevation:160, EndElevation:320