C#比较实体与实体或集合实体与集合实体的数据比较

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/// <summary>
/// 实体类的新旧值比较方法(参数1 新的实体类,参数二 旧实体类,参数三 需要比较的属性的集合,参数四 需要比较的集合的显示值)
/// </summary>
/// <param name="newAverage">新实体类</param>
/// <param name="oldAverage">旧实体类</param>
/// <param name="compareAttribute">需要比较的属性集合</param>
/// <param name="compareAttributeName">需要比较的属性集合的中文名集合</param>
/// <returns>返回有差异的数据的拼接字符串</returns>
public static string ModelCompare(object newAverage, object oldAverage, List<string> compareAttribute, List<string> compareAttributeName)
{
    if (newAverage == null || oldAverage == null)
    {
        return "传入比较值为空,请检查传入值!";
    }
    if (compareAttribute.Count() != compareAttributeName.Count())
    {
        return "传入的要比较的列与对应的显示值数量不等,请检查要比较的列的数量与对应显示列的数量!";
    }
    string differentThing = null;
    List<object> newValue = new List<object>();
    List<object> oldValue = new List<object>();
    //判断是否有修改
    bool isDiffOrNot = false;
    try
    {
        // 只有两个对象都是同一类型的才有可比性
        if (newAverage.GetType() == oldAverage.GetType())
        {
            foreach (string item in compareAttribute)
            {
                //获取新值的要比较项的集合
                foreach (PropertyInfo pi in newAverage.GetType().GetProperties())
                {
                    string name = pi.Name.ToString();
                    object value = pi.GetValue(newAverage, null);
                    if (item.Equals(name))
                    {
                        newValue.Add(value);
                        break;
                    }
                }
            }
            foreach (var item in compareAttribute)
            {
                //获取旧值的要比较项的集合
                foreach (PropertyInfo pi in oldAverage.GetType().GetProperties())
                {
                    string name = pi.Name;
                    object value = pi.GetValue(oldAverage, null);
                    if (item.Equals(name))
                    {
                        oldValue.Add(value);
                        break;
                    }
                }
            }
            //新旧值比较,有不同的就添加到返回值中去
            for (int i = 0; i < newValue.Count(); i++)
            {
                if (string.IsNullOrWhiteSpace(Convert.ToString(newValue[i])) && string.IsNullOrWhiteSpace(Convert.ToString(oldValue[i])))
                {
                }
                else if (!string.IsNullOrWhiteSpace(Convert.ToString(newValue[i])) && string.IsNullOrWhiteSpace(Convert.ToString(oldValue[i])))
                {
                    isDiffOrNot = true;
                    differentThing += compareAttributeName[i] + ":无数据-->>" + newValue[i] + ";";
                }
                else if (string.IsNullOrWhiteSpace(Convert.ToString(newValue[i])) && !string.IsNullOrWhiteSpace(Convert.ToString(oldValue[i])))
                {
                    isDiffOrNot = true;
                    differentThing += compareAttributeName[i] + ":" + oldValue[i] + "-->>无数据;";
                }
                else
                {
                    if (!newValue[i].Equals(oldValue[i]))
                    {
                        isDiffOrNot = true;
                        differentThing += compareAttributeName[i] + ":" + oldValue[i] + "-->>" + newValue[i] + ";";
                    }
                }
            }
            if (!isDiffOrNot)
                differentThing = "新旧值无变化!";
        }
        else
            differentThing = "请输入相同类型进行比较!";
    }
    catch (Exception ex)
    {
        //Common.WriteDayLog("ModelCompare\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n" + ex.Message + "\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n" + DateTime.Now);
        throw;
    }
    return differentThing;
}
 
/// <summary>
/// 集合类的新旧值比较方法(参数1 新的实体类的集合,参数二 旧实体类的集合,参数三 需要比较的属性的集合,参数四 需要比较的集合的显示值)
/// </summary>
/// <param name="newAverages">新集合类</param>
/// <param name="oldAverages">旧集合类</param>
/// <param name="compareAttribute">需要比较的属性的集合</param>
/// <param name="compareAttributeName">需要比较的属性集合的中文名集合</param>
/// <param name="matchAveragesAttribute">用来整理新旧集合的属性名</param>
/// <returns>返回有差异的数据的拼接字符串的集合</returns>
public static List<string> LstCompare<T>(List<T> newAverages, List<T> oldAverages, List<string> compareAttribute, List<string> compareAttributeName, string matchAveragesAttribute)
{
    List<string> differentThings = new List<string>();
    if (newAverages.Count() == 0 || oldAverages.Count() == 0)
    {
        differentThings.Add("传入比较的集合有一个或多个为空,请检查传入值!");
    }
    if (compareAttribute.Count() != compareAttributeName.Count())
    {
        differentThings.Add("传入的要比较的列与对应的显示值数量不等,请检查要比较的列的数量与对应显示列的数量!");
    }
    //数量不等时调用matchAveragesAttribute整理新旧集合数量
    int addAverages = 0;//指定多出来的数据列是添加还是删除(0、不变;1、添加;2、删除)
    List<T> diffAttributeValue = new List<T>();//原集合中多出来的数据列
    if (newAverages.Count() != oldAverages.Count())
    {
        //开始匹配整理集合
        if (newAverages.Count() > oldAverages.Count())
        {
            addAverages = 1;
            diffAttributeValue = NotFindAverages(newAverages.ToList(), oldAverages.ToList(), matchAveragesAttribute);
            foreach (var item in diffAttributeValue)
            {
                newAverages.Remove(item);
            }
        }
        else
        {
            addAverages = 2;
            diffAttributeValue = NotFindAverages(oldAverages.ToList(), newAverages.ToList(), matchAveragesAttribute);
            foreach (var item in diffAttributeValue)
            {
                oldAverages.Remove(item);
            }
        }
    }
 
    int count = 0;
    int countdiff = 1;
 
    try
    {
        //集合内包含的数据列一样多才有意义
        if (newAverages.Count() == oldAverages.Count())
        {
            foreach (var item in newAverages)
            {
                string diffT = ModelCompare(item, oldAverages[count], compareAttribute, compareAttributeName);
                if (diffT.Contains(";"))
                {
                    diffT = "【第" + countdiff + "条修改项】" + diffT;
                    differentThings.Add(diffT);
                    countdiff++;
                }
                count++;
            }
        }
        string addOrDelete = null;
        if (addAverages == 1)
        {
            addOrDelete = SplicingAveragesValue(diffAttributeValue, compareAttribute, compareAttributeName);
            differentThings.Add("【添加】;" + addOrDelete);
        }
        else if (addAverages == 2)
        {
            addOrDelete = SplicingAveragesValue(diffAttributeValue, compareAttribute, compareAttributeName);
            differentThings.Add("【删除】;" + addOrDelete);
        }
    }
    catch (Exception ex)
    {
        //Common.WriteDayLog("LstCompare\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n" + ex.Message + "\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n" + DateTime.Now);
        throw;
    }
    return differentThings;
}
 
/// <summary>
/// 根据标志属性筛选出两个集合类中不同的类的集合(参数1 数量较多的集合,参数二 数量较少的集合,参数三 标志属性)
/// </summary>
/// <param name="bigAverages">数量较多的集合</param>
/// <param name="smallAverages">数量较少的集合</param>
/// <param name="matchAveragesAttribute">标志属性</param>
/// <returns>返回找出来的类的集合</returns>
public static List<T> NotFindAverages<T>(List<T> bigAverages, List<T> smallAverages, string matchAveragesAttribute)
{
    List<T> diffAttributeValue = new List<T>();
    foreach (var item in bigAverages)
    {
        foreach (PropertyInfo pi in item.GetType().GetProperties())
        {
            string name = pi.Name.ToString();
            object value = pi.GetValue(item, null);
            if (matchAveragesAttribute.Equals(name))
            {
                bool isNotFind = true;
                foreach (var item2 in smallAverages)
                {
                    foreach (PropertyInfo pi2 in item2.GetType().GetProperties())
                    {
                        string name2 = pi2.Name.ToString();
                        object value2 = pi2.GetValue(item2, null);
                        if (matchAveragesAttribute.Equals(name2))
                        {
                            if (value.ToString() == value2.ToString())
                            {
                                isNotFind = false;
                            }
                            break;
                        }
                    }
                    if (!isNotFind)
                    {
                        break;
                    }
                }
                if (isNotFind)
                {
                    diffAttributeValue.Add(item);
                }
                break;
            }
        }
    }
    return diffAttributeValue;
}
 
/// <summary>
/// 拼接多余的实体类与显示属性对应成字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="diffAttributeValue"></param>
/// <param name="compareAttribute"></param>
/// <param name="compareAttributeName"></param>
/// <returns></returns>
public static string SplicingAveragesValue<T>(List<T> diffAttributeValue, List<string> compareAttribute, List<string> compareAttributeName)
{
    string addOrDelete = null;
    try
    {
        foreach (var item3 in diffAttributeValue)
        {
            int count = 0;
            foreach (string item in compareAttribute)
            {
                foreach (PropertyInfo pi in item3.GetType().GetProperties())
                {
                    string name = pi.Name.ToString();
                    object value = pi.GetValue(item3, null);
                    if (item.Equals(name))
                    {
                        addOrDelete += compareAttributeName[count] + ":" + value + ";";
                        break;
                    }
                }
                count++;
            }
        }
    }
    catch (Exception ex)
    {
        addOrDelete = ex.Message;
        throw;
    }
    return addOrDelete;
}

  

posted @   勤奋的二牛  阅读(467)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示