技术学习

我所喜欢的

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  135 随笔 :: 5 文章 :: 6 评论 :: 11万 阅读
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/// <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   飘扬De黑夜  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2019-12-26 C# 扩展
点击右上角即可分享
微信分享提示