schangxiang@126.com
2024-09-06 2a19504209e763a7c0e957e4ee265dd419486ef1
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
using System.Linq.Expressions;
using System.Reflection;
 
namespace Furion.Extras.iWare.Wms.Util
{
    public class DataCompareUtil<T1, T2>
    {
        public DataCompareUtil(
            Expression<Func<T1, object>> SelectorKey_1,
            Expression<Func<T2, object>> SelectorKey_2
            )
        {
            Propertys = new List<View_Property>();
            T_1 = typeof(T1);
            T_2 = typeof(T2);
 
            KeyProperty = new View_Property()
            {
                Property_1 = T_1.GetProperty(GetPropertyName(SelectorKey_1)),
                Property_2 = T_2.GetProperty(GetPropertyName(SelectorKey_2))
            };
        }
 
        private List<View_Property> Propertys { get; set; }
 
        private Type T_1 { get; set; }
        private Type T_2 { get; set; }
 
        private View_Property KeyProperty { get; set; }
 
        public void PushCompare(
            Expression<Func<T1, object>> Selector_1,
            Expression<Func<T2, object>> Selector_2
            )
        {
            Propertys.Add(new View_Property()
            {
                Property_1 = T_1.GetProperty(GetPropertyName(Selector_1)),
                Property_2 = T_2.GetProperty(GetPropertyName(Selector_2))
            });
        }
 
        public View_DataCompare<T1, T2> Compare(
            List<T1> Data_1,
            List<T2> Data_2
            )
        {
            View_DataCompare<T1, T2> data = new View_DataCompare<T1, T2>();
 
            Dictionary<string, T1> dic_Contain_2 = new Dictionary<string, T1>();
            Dictionary<string, T2> dic_Contain_1 = new Dictionary<string, T2>();
 
            foreach (var item_1 in Data_1)
            {
                foreach (var item_2 in Data_2)
                {
                    bool IsDifferent = false;
 
                    foreach (var Property_item in Propertys)
                    {
                        if (!IsDifferent)
                        {
                            if (Property_item.Property_1 == null || Property_item.Property_2 == null)
                            {
                                if (Property_item.Property_1 == null && Property_item.Property_2 == null)
                                {
                                    break;
                                }
                                else
                                {
                                    IsDifferent = true;
                                }
                            }
                            else
                            {
                                var obj_value1 = Property_item.Property_1.GetValue(item_1);
                                var obj_value2 = Property_item.Property_2.GetValue(item_2);
 
                                if (obj_value1 != null && obj_value2 != null)
                                {
                                    if (obj_value1 is decimal @decimal && obj_value2 is decimal @decimal2 && @decimal != @decimal2)
                                    {
                                        IsDifferent = true;
                                    }
                                    else if (obj_value1 is int @intl && obj_value2 is int @int2 && @intl != @int2)
                                    {
                                        IsDifferent = true;
                                    }
                                    else if (obj_value1 is Guid @Guidl && obj_value2 is Guid @Guid2 && @Guidl != @Guid2)
                                    {
                                        IsDifferent = true;
                                    }
                                    else if (obj_value1 is DateTime @DateTimel && obj_value2 is DateTime @DateTime2 && @DateTimel != @DateTime2)
                                    {
                                        IsDifferent = true;
                                    }
                                    else
                                    {
                                        if (obj_value1.ToString() != obj_value2.ToString())
                                            IsDifferent = true;
                                    }
                                }
                                else if (obj_value1 != null || obj_value2 != null)
                                {
                                    IsDifferent = true;
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
 
                    if (!IsDifferent)
                    {
                        //item_1与item_2相同
                        string key_1 = KeyProperty.Property_1.GetValue(item_1).ToString();
 
                        if (!dic_Contain_2.ContainsKey(key_1))
                        {
                            dic_Contain_2.Add(key_1, item_1);
                            data.Contain_2.Add(item_1);
                        }
 
                        string key_2 = KeyProperty.Property_2.GetValue(item_2).ToString();
 
                        if (!dic_Contain_1.ContainsKey(key_2))
                        {
                            dic_Contain_1.Add(key_2, item_2);
                            data.Contain_1.Add(item_2);
                        }
                    }
                }
            }
 
            foreach (var item_1 in Data_1)
            {
                string key_1 = KeyProperty.Property_1.GetValue(item_1).ToString();
 
                if (!dic_Contain_2.ContainsKey(key_1))
                {
                    data.NoContain_2.Add(item_1);
                }
            }
 
            foreach (var item_2 in Data_2)
            {
                string key_2 = KeyProperty.Property_2.GetValue(item_2).ToString();
 
                if (!dic_Contain_1.ContainsKey(key_2))
                {
                    data.NoContain_1.Add(item_2);
                }
            }
 
            return data;
        }
 
        public static string GetPropertyName<T>(Expression<Func<T, object>> expr)
        {
            var rtn = "";
            if (expr.Body is UnaryExpression expression)
            {
                rtn = ((MemberExpression)expression.Operand).Member.Name;
            }
            else if (expr.Body is MemberExpression expression2)
            {
                rtn = expression2.Member.Name;
            }
            else if (expr.Body is ParameterExpression expression1)
            {
                rtn = expression1.Type.Name;
            }
            return rtn;
        }
    }
 
    public class View_DataCompare<T1, T2>
    {
        public View_DataCompare()
        {
            Contain_2 = new List<T1>();
            Contain_1 = new List<T2>();
            NoContain_2 = new List<T1>();
            NoContain_1 = new List<T2>();
        }
 
        /// <summary>
        /// 2号数据源中存在
        /// </summary>
        public List<T1> Contain_2 { get; set; }
 
        /// <summary>
        /// 1号数据源中存在
        /// </summary>
        public List<T2> Contain_1 { get; set; }
 
        /// <summary>
        /// 2号数据源中不存在
        /// </summary>
        public List<T1> NoContain_2 { get; set; }
 
        /// <summary>
        /// 1号数据源中不存在
        /// </summary>
        public List<T2> NoContain_1 { get; set; }
    }
 
    public class View_Property
    {
        public PropertyInfo Property_1 { get; set; }
        public PropertyInfo Property_2 { get; set; }
    }
}