Skip to content

Instantly share code, notes, and snippets.

@cleytonferrari
Created January 21, 2013 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cleytonferrari/4586685 to your computer and use it in GitHub Desktop.
Save cleytonferrari/4586685 to your computer and use it in GitHub Desktop.
Usa o agrupamento para fazer um Distinct de objetos complexo, mais informações em http://stackoverflow.com/questions/489258/linq-distinct-on-a-particular-property
//Selecionando objetos distintos usando Linq
//Agrupando com uma propriedade
List<Pessoa> pessoasDistintas = todasAsPessoas
.GroupBy(p => p.Id)
.Select(g => g.First())
.ToList();
//Agrupando com varias propriedades
List<Pessoa> pessoasDistintas = todasAsPessoas
.GroupBy(p => new {p.Id, p.CorFavorita} )
.Select(g => g.First())
.ToList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment