Guest User

Untitled

a guest
Sep 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. Grouping / Multiple grouping with LINQ
  2. Best Type Name
  3. - Discipline name
  4. -- Result
  5. -- Result
  6. -- Result
  7.  
  8. public class CompetitorBest
  9. {
  10. public int ResultId { get; set; }
  11. public string BestTypeName { get; set; }
  12. public int BestTypeOrder { get; set; }
  13. public string DisciplineName { get; set; }
  14. public string ResultValue { get; set; }
  15. public string Venue { get; set; }
  16. public DateTime ResultDate { get; set; }
  17. }
  18.  
  19. var bestsGroups = from b in Model.CompetitorBests
  20. group b by new { b.BestTypeName, b.BestTypeOrder }
  21. into grp
  22. orderby grp.Key.BestTypeOrder
  23. select new
  24. {
  25. BestType = grp.Key.BestTypeName,
  26. Results = grp.ToList()
  27. };
  28.  
  29. foreach (var bestsGroup in bestsGroups)
  30. {
  31. <h2>@bestsGroup.BestType</h2>
  32.  
  33. foreach (var result in bestsGroup.Results)
  34. {
  35. //i am guessing here i'll need another foreach on the discipline group....
  36. <p>@result.DisciplineName</p>
  37. <p>@result.ResultId </p>
  38. }
  39. }
  40.  
  41. from b in Model.CompetitorBests
  42. group b by new { b.BestTypeName, b.BestTypeOrder } into grp
  43. orderby grp.Key.BestTypeOrder
  44. select new
  45. {
  46. BestType = grp.Key.BestTypeName,
  47. Results = from d in grp
  48. group d by d.DisciplineName into grp2
  49. select new
  50. {
  51. DisciplineName = grp2.Key,
  52. Results = grp2
  53. }
  54. };
  55.  
  56. foreach (var bestsGroup in bestsGroups)
  57. {
  58. <h2>@bestsGroup.BestType</h2>
  59.  
  60. foreach (var discipline in bestsGroup.Results)
  61. {
  62. <p>@discipline.DisciplineName</p>
  63.  
  64. foreach (var result in discipline.Results)
  65. {
  66. <p>@result.ResultId</p>
  67. }
  68. }
  69. }
Add Comment
Please, Sign In to add comment