//深拷贝对象
/// <summary> /// 深拷贝对象:循环List是直接改变当前对象的值,所以如果需要改变值且不动到当前list需要深拷贝, 因为是引用类型最终都是指向一个内存地址 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="original"></param> /// <returns></returns> private static T DeepCopy<T>(T original) { return (T)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(original), original.GetType()); }
//求两个List<实体>之间的差集
List<A> A = new List<A>(); List<B> B= new List<B>(); public class AEquality : IEqualityComparer<A> { public bool Equals(A x, A y) { return x.Id == y.Id && x.Name == y.Name } public int GetHashCode(A obj) { return (obj == null) ? 0 : obj.ToString().GetHashCode(); } } var except = A.Except(B, new AEquality()).ToList(); if (except.Count() > 0) throw JNPFException.Oh("存在差集"); //C类是A类的子类 可直接使用AEquality 如下 var except = C.Except(B, new AEquality()).ToList(); if (except.Count() > 0) throw JNPFException.Oh("存在差集");