c#:两个list不同元素,值类型,对象类型

2023-08-24 14:32:04 浏览数 (1)

基本值类型

代码语言:javascript复制
    public static List<T> GetListDiff<T>(List<T> listA,List<T> listB)
    {
        List<T> ret = new List<T>();
        if (listA.Count > listB.Count)
            ret = listA.Except(listB).ToList();
        else {
            ret = listB.Except(listA).ToList();
        }
        return ret;
    }

对象类型

代码语言:javascript复制
public class DiffDevlopments : IEqualityComparer<Devlopments>
{
    public bool Equals(Devlopments x, Devlopments y)
    {
        return x.id == y.id;
    }

    public int GetHashCode(Devlopments obj)
    {
        if (obj == null)
        {
            return 0;
        }
        else
        {
            return obj.ToString().GetHashCode();
        }
    }
}
代码语言:javascript复制
 List<Devlopments> listDiff = DataMgr.m_listDevlopments.Except(newDeve, new DiffDevlopments()).ToList();//差集

注意:长的list在前

0 人点赞