fork(1) download
  1. using static System.Console;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program {
  6. public static void Main() {
  7. List<Pet> pets =
  8. new List<Pet>{ new Pet { Name="Barley", Age=8 },
  9. new Pet { Name="Boots", Age=4 },
  10. new Pet { Name="Whiskers", Age=1 },
  11. new Pet { Name="Daisy", Age=4 } };
  12. //está agrupando por idade e depois po nome.
  13. //Como existe dois pets com mesma idade eles serão agrupados
  14. var query = pets.GroupBy(pet => pet.Age, pet => pet.Name);
  15. foreach (var petGroup in query) {
  16. WriteLine(petGroup.Key);
  17. foreach (var name in petGroup) WriteLine(" {0}", name);
  18. }
  19. }
  20. }
  21.  
  22. class Pet {
  23. public string Name { get; set; }
  24. public int Age { get; set; }
  25. }
  26.  
  27. //https://pt.stackoverflow.com/q/82746/101
Success #stdin #stdout 0.02s 17312KB
stdin
Standard input is empty
stdout
8
  Barley
4
  Boots
  Daisy
1
  Whiskers