技术学习

我所喜欢的

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
/// <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;
        }

  

posted on 2023-12-26 14:34  飘扬De黑夜  阅读(1)  评论(0编辑  收藏  举报