LINQ解決依據某個字段去重


想要List結果反復 的數據非常easy。僅僅要.Dinstinct()就好了


可是假設想要依據某個字段去除反復的數據,上面的方法就幫不上忙了。我們須要重寫一個方法。直接上樣例吧

[Serializable]
    public class HomePageUserModel
    {
        public int UserID { get; set; }
        public string TitleUserName { get; set; }
        public string ShowUserName { get; set; }
        public DateTime?

ExcuteTime { get; set; } } [Serializable] public class HomePageUserModelComparer : EqualityComparer<HomePageUserModel> { public override bool Equals(HomePageUserModel x,HomePageUserModel y) { if (x == null && y == null) { return false; } else { return x.TitleUserName == y.TitleUserName; } } public override int GetHashCode(HomePageUserModel obj) { return obj.TitleUserName.GetHashCode(); } }




這兒調用就能夠完畢自己定義字段去重的效果



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM