Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get distinct items from a list

Tags:

c#

list

linq

I have a list of objects with three integer properties. How can I get the distinct values of first integer property from my list?

like image 412
Mazdak Avatar asked Sep 01 '25 22:09

Mazdak


2 Answers

This should work,

List<int> result = YourListObject.Select(o => o.FirstInteger).Distinct().ToList();
like image 66
A_Nabelsi Avatar answered Sep 03 '25 13:09

A_Nabelsi


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.

like image 34
Jason Evans Avatar answered Sep 03 '25 14:09

Jason Evans