/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sources"></param> /// <param name="details"></param> /// <param name="mdRelation"></param> /// <returns></returns> public static List<T> Intersect<T>(this List<T> sources, List<T> details, Func<T, T, bool> compareCond) { List<T> result = new List<T>(); sources?.ForEach(item => { details?.ForEach(detail => { if (compareCond(detail, item)) { result.Add(item); return; } }); }); return result; } /// <summary> /// 取差集合 A-B,A集合中有B集合中无的 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sources"></param> /// <param name="details"></param> /// <param name="compareCond"></param> /// <returns></returns> public static List<T> Expect<T>(this List<T> sources, List<T> details, Func<T, T, bool> compareCond) { List<T> result = new List<T>(); sources?.ForEach(item => { if (!details.Exist<T>(item, compareCond)) { result.Add((T)item); } }); return result; } /// <summary> /// 取并集(A和B取并集,共通的项目选A) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="entity"></param> /// <param name="compareCond"></param> /// <returns></returns> public static List<T> Union<T>(this List<T> source, List<T> target, Func<T, T, bool> compareCond) { List<T> result = new List<T>(); result.AddRange(source); target?.ForEach(item => { if (!target.Exist<T>(item, compareCond)) { result.Add(item); } }); return result; } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns></returns> public static bool Exist<T>(this List<T> source, T entity,Func<T, T, bool> compareCond) { bool bExist = false; source.ForEach(item => { if (compareCond(item, entity)) { bExist = true; return; } }); return bExist; }
标签:compareCond,return,C#,List,扩展,item,result,集合,ForEach From: https://www.cnblogs.com/volts0302/p/17928072.html