I have a list of objects with three integer properties. How can I get the distinct values of first integer property from my list?
4 Answers
This should work,
List<int> result = YourListObject.Select(o => o.FirstInteger).Distinct().ToList(); 1 Comment
Sai
+1 List<int> result = YourListObject.Select(o => o.FirstInteger).AsParallel().Distinct().ToList() "AsParallel()" might give some performance benfit, if we doesn't care about order and have more items in the list.
Try:
var g = collection.Select(i => i.Property1).Distinct();
Could you post some source code so that we can give you a better example?
EDIT:
In my example, I have a collection collection which contains numerous instances of your class. I'm then selecting Property1 from each class, filtering to the distinct values of that property.
Comments
Example of a more complex distinct'ing....
licenseLookupItems = tmpList .GroupBy(x => new {x.LicenseNumber, x.Name, x.Location, x.Active, x.Archived}) .Select(p => p.FirstOrDefault()) .Select(p => new LicenseNumberLookupItem { LicenseNumber = p.LicenseNumber, Name = p.Name, Location = p.Location, Active = p.Active, Archived = p.Archived }) .ToList(); 1 Comment
DRapp
Nice to pull multiple distinct parts of a record