欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-25 11:37:22
复制代码 代码如下:public partial class v_myjoinedcloudrespository {     ...

复制代码 代码如下:

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();
        }
    }