首页 > 编程 > C# > 正文

泛型编程去掉字段重复数据的方法

2020-01-24 02:54:56
字体:
来源:转载
供稿:网友

复制代码 代码如下:

public partial class V_MyJoinedCloudRespository

{

        public List<V_MyJoinedCloud> GetDistinctJoinedCloud(string cloudName,long ExpertId)
        {
            var lamd = from p in db.V_MyJoinedCloud
                       where p.Status == 1 && p.CompanyName.Contains(cloudName) && p.ExpertID==ExpertId
                       select p;

            List<V_MyJoinedCloud> list = new List<V_MyJoinedCloud>();
            if (lamd!=null&&lamd.Count()>0)
            {
                list = lamd.ToList();
                list = list.Distinct(new CompanyNameComparer()).ToList();
            }
            return list;
        }

}

public class CompanyNameComparer : IEqualityComparer<V_MyJoinedCloud>
    {
        public bool Equals(V_MyJoinedCloud x, V_MyJoinedCloud y)
        {
            if (x == null)
                return y == null;
            return x.CompanyID == y.CompanyID;
        }

        public int GetHashCode(V_MyJoinedCloud obj)
        {
            if (obj == null)
                return 0;
            return obj.CompanyID.GetHashCode();
        }
    }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表