LINQ: Distinct values

Are you trying to be distinct by more than one field? If so, just use an anonymous type and the Distinct operator and it should be okay: var query = doc.Elements(“whatever”) .Select(element => new { id = (int) element.Attribute(“id”), category = (int) element.Attribute(“cat”) }) .Distinct(); If you’re trying to get a distinct set of values … Read more

LINQ’s Distinct() on a particular property

What if I want to obtain a distinct list based on one or more properties? Simple! You want to group them and pick a winner out of the group. List<Person> distinctPeople = allPeople .GroupBy(p => p.PersonId) .Select(g => g.First()) .ToList(); If you want to define groups on multiple properties, here’s how: List<Person> distinctPeople = allPeople … Read more