Lambda Distinct Select
This question already has an answer here:
If you don't override Equals
and GetHashCode
in your class or provide a custom equality comparer Distinct
method uses default equality comparer for the type.And it compares reference types by references. Not by the property values. If you don't want this behaviour either override the relevant methods in your class or if you can't change the class implement an IEqualityComparer
for the type and pass it to Distinct
:
var ResourceTypeNameList = Resources
.Select(r => new bl_SelectBox{ text=r.ResourceTypeName, value=r.resourceTypeID })
.Distinct(new MyEqualityComparer());
Another quick solution would be using GroupBy
:
var ResourceTypeNameList = Resources
.Select(r => new bl_SelectBox{ text=r.ResourceTypeName, value=r.resourceTypeID })
.GroupBy(x => x.SomeProperty)
.Select(x => x.First());
链接地址: http://www.djcxy.com/p/51292.html
上一篇: Linq获得独特的财产
下一篇: Lambda独特选择