You're asking about getting unique values of 'Name' from my newSpecs collection, but your code returns IEnumerable<SpecialtyData>
. Something is wrong: either the question or the return type.
If you need only names, you need IEnumerable<string>
instead:
IEnumerable<string> distinctNames = newSpecs
.Select(s => s.Name)
.Distinct().OrderBy(x => x);
If you need entire SpecialtyData
instances, you can either use GroupBy
+First
:
IEnumerable<SpecialtyData> distinctSpecialties = newSpecs
.GroupBy(s => s.Name)
.Select(g=> g.First())
or override Equals
and GetHashCode
methods within SpecialtyData
class to make Distinct
work.