天天看點

c#:兩個list不同元素,值類型,對象類型

基本值類型

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;
    }      
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();
        }
    }
}      
List<Devlopments> listDiff = DataMgr.m_listDevlopments.Except(newDeve, new DiffDevlopments()).ToList();//差集