经常用到数据去重复

趁着今天不想干别的就整理一下好了

直接上代码:

首先写一个比较帮助类

 public class ComparerAnswerHelper : IEqualityComparer<AnswerEntity>
        {
            public bool Equals(AnswerEntity x,AnswerEntity y)
            {
                return x.AnswerContent == y.AnswerContent;
            }
            public int GetHashCode(AnswerEntity obj)
            {
                return obj.AnswerContent.GetHashCode();
            }
        }

想要去重什么字段就在Equals方法中比较什么字段

如上是去重复AnswerContent.

在控制器中进行调用时:

 this.HosService.GetAnswerList(request).Distinct(new HOSBS.Framework.Web.Controls.CheckBoxListHelper.ComparerAnswerHelper())

调用 实体.Distinct(new ComparerAnswerHelper())