這是我用來測(cè)試的代碼,T_ZJ_PostTouch還有其他字段
List<T_ZJ_PostTouch> list = new List<T_ZJ_PostTouch> { new T_ZJ_PostTouch{OrgCode="1"}, new T_ZJ_PostTouch{OrgCode="1"}, new T_ZJ_PostTouch{OrgCode="2"}, new T_ZJ_PostTouch{OrgCode="2"}, };
?
int count = list.Distinct().Count();//結(jié)果是4 int count1 = list.Distinct(new OrgCodeComparer()).Count();//結(jié)果是2
?
public class OrgCodeComparer : IEqualityComparer<T_ZJ_PostTouch> { public bool Equals(T_ZJ_PostTouch x, T_ZJ_PostTouch y) { if (x == null) return y == null; return x.OrgCode == y.OrgCode; }
public int GetHashCode(T_ZJ_PostTouch obj) { if (obj == null) return 0; return obj.OrgCode.GetHashCode(); } }
1 回答

鴻蒙傳說
TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
這很正常,因?yàn)槟阌肈istinct()方法時(shí),默認(rèn)的Comparer是EqualityComparer<T_ZJ_PostTouch>.Default 這個(gè)comparer,對(duì)于大部分引用類型,比較的是引用。而你自己的 OrgCodeComparer比較的是OrgCode字段。
- 1 回答
- 0 關(guān)注
- 394 瀏覽
添加回答
舉報(bào)
0/150
提交
取消