List Distinct根据实体属性去重
集合中全是实体,怎么根据实体某一个属性来对比唯一性?
public class PeopleCompareByID : IEqualityComparer<CalendarResult> { public bool Equals(CalendarResult x, CalendarResult y) { if (x == null || y == null) return false; if (x.SC_ID == y.SC_ID) return true; else return false; } public int GetHashCode(CalendarResult obj) { if (obj == null) return 0; else return obj.SC_ID.GetHashCode(); } }
实体:CalendarResult 唯一属性:SC_ID 这是对比一个属性;
当然也可以是多个,只需稍加调整部分代码: A. if (x.SC_ID == y.SC_ID && x.Account == y.Account)
B. return obj.SC_ID.GetHashCode()^ obj.Account.GetHashCode();