Distinct()
아래의 코드를 찾을 수 있습니다. 적절한 IEqualityComparer
을 정의하고 메서드에 전달하여 Distinct
에 기본 Equals(object obj)
메서드를 사용하지 말고 자신의 이퀄라이제이션을 지정해야합니다.
class DropDownItemEqualityComparer : IEqualityComparer<DropDownItem>
{
#region IEqualityComparer<DropDownItem> Member
public bool Equals(DropDownItem x, DropDownItem y)
{
return
x == null || y == null
? false
: x.Value == y.Value && x.Text == y.Text;
//Or whatever properties you want to be equal
}
public int GetHashCode(DropDownItem obj)
{
if (obj != null)
return
obj.Value.GetHashCode() + obj.Text.GetHashCode();
else
throw new ArgumentNullException("obj");
}
#endregion
}
그런 다음이 작동하고 Distinct
방법
var SalesOrgTypes = (from s in tblSales
orderby s.SalesOrg ascending
select new DropDownItem
{
Value = s.SalesOrg,
Text = s.SalesOrg + " - " + s.SalesOrgDesc
}).Distinct(new DropDownItemEqualityComparer());
당신은 실제로 무슨 일이 일어나고 있는지 말했다하지 않은 ... –