DataCompareUtil.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. namespace YBEE.EQM.Core
  6. {
  7. public class DataCompareUtil<T1, T2>
  8. {
  9. public DataCompareUtil(
  10. Expression<Func<T1, object>> SelectorKey_1,
  11. Expression<Func<T2, object>> SelectorKey_2
  12. )
  13. {
  14. Propertys = new List<View_Property>();
  15. T_1 = typeof(T1);
  16. T_2 = typeof(T2);
  17. KeyProperty = new View_Property()
  18. {
  19. Property_1 = T_1.GetProperty(GetPropertyName(SelectorKey_1)),
  20. Property_2 = T_2.GetProperty(GetPropertyName(SelectorKey_2))
  21. };
  22. }
  23. private List<View_Property> Propertys { get; set; }
  24. private Type T_1 { get; set; }
  25. private Type T_2 { get; set; }
  26. private View_Property KeyProperty { get; set; }
  27. public void PushCompare(
  28. Expression<Func<T1, object>> Selector_1,
  29. Expression<Func<T2, object>> Selector_2
  30. )
  31. {
  32. Propertys.Add(new View_Property()
  33. {
  34. Property_1 = T_1.GetProperty(GetPropertyName(Selector_1)),
  35. Property_2 = T_2.GetProperty(GetPropertyName(Selector_2))
  36. });
  37. }
  38. public View_DataCompare<T1, T2> Compare(
  39. List<T1> Data_1,
  40. List<T2> Data_2
  41. )
  42. {
  43. View_DataCompare<T1, T2> data = new View_DataCompare<T1, T2>();
  44. Dictionary<string, T1> dic_Contain_2 = new Dictionary<string, T1>();
  45. Dictionary<string, T2> dic_Contain_1 = new Dictionary<string, T2>();
  46. foreach (var item_1 in Data_1)
  47. {
  48. foreach (var item_2 in Data_2)
  49. {
  50. bool IsDifferent = false;
  51. foreach (var Property_item in Propertys)
  52. {
  53. if (!IsDifferent)
  54. {
  55. if (Property_item.Property_1 == null || Property_item.Property_2 == null)
  56. {
  57. if (Property_item.Property_1 == null && Property_item.Property_2 == null)
  58. {
  59. break;
  60. }
  61. else
  62. {
  63. IsDifferent = true;
  64. }
  65. }
  66. else
  67. {
  68. var obj_value1 = Property_item.Property_1.GetValue(item_1);
  69. var obj_value2 = Property_item.Property_2.GetValue(item_2);
  70. if (obj_value1 != null && obj_value2 != null)
  71. {
  72. if (obj_value1 is decimal @decimal && obj_value2 is decimal @decimal2 && @decimal != @decimal2)
  73. {
  74. IsDifferent = true;
  75. }
  76. else if (obj_value1 is int @intl && obj_value2 is int @int2 && @intl != @int2)
  77. {
  78. IsDifferent = true;
  79. }
  80. else if (obj_value1 is Guid @Guidl && obj_value2 is Guid @Guid2 && @Guidl != @Guid2)
  81. {
  82. IsDifferent = true;
  83. }
  84. else if (obj_value1 is DateTime @DateTimel && obj_value2 is DateTime @DateTime2 && @DateTimel != @DateTime2)
  85. {
  86. IsDifferent = true;
  87. }
  88. else
  89. {
  90. if (obj_value1.ToString() != obj_value2.ToString())
  91. IsDifferent = true;
  92. }
  93. }
  94. else if (obj_value1 != null || obj_value2 != null)
  95. {
  96. IsDifferent = true;
  97. }
  98. }
  99. }
  100. else
  101. {
  102. break;
  103. }
  104. }
  105. if (!IsDifferent)
  106. {
  107. //item_1与item_2相同
  108. string key_1 = KeyProperty.Property_1.GetValue(item_1).ToString();
  109. if (!dic_Contain_2.ContainsKey(key_1))
  110. {
  111. dic_Contain_2.Add(key_1, item_1);
  112. data.Contain_2.Add(item_1);
  113. }
  114. string key_2 = KeyProperty.Property_2.GetValue(item_2).ToString();
  115. if (!dic_Contain_1.ContainsKey(key_2))
  116. {
  117. dic_Contain_1.Add(key_2, item_2);
  118. data.Contain_1.Add(item_2);
  119. }
  120. }
  121. }
  122. }
  123. foreach (var item_1 in Data_1)
  124. {
  125. string key_1 = KeyProperty.Property_1.GetValue(item_1).ToString();
  126. if (!dic_Contain_2.ContainsKey(key_1))
  127. {
  128. data.NoContain_2.Add(item_1);
  129. }
  130. }
  131. foreach (var item_2 in Data_2)
  132. {
  133. string key_2 = KeyProperty.Property_2.GetValue(item_2).ToString();
  134. if (!dic_Contain_1.ContainsKey(key_2))
  135. {
  136. data.NoContain_1.Add(item_2);
  137. }
  138. }
  139. return data;
  140. }
  141. public static string GetPropertyName<T>(Expression<Func<T, object>> expr)
  142. {
  143. var rtn = "";
  144. if (expr.Body is UnaryExpression expression)
  145. {
  146. rtn = ((MemberExpression)expression.Operand).Member.Name;
  147. }
  148. else if (expr.Body is MemberExpression expression2)
  149. {
  150. rtn = expression2.Member.Name;
  151. }
  152. else if (expr.Body is ParameterExpression expression1)
  153. {
  154. rtn = expression1.Type.Name;
  155. }
  156. return rtn;
  157. }
  158. }
  159. public class View_DataCompare<T1, T2>
  160. {
  161. public View_DataCompare()
  162. {
  163. Contain_2 = new List<T1>();
  164. Contain_1 = new List<T2>();
  165. NoContain_2 = new List<T1>();
  166. NoContain_1 = new List<T2>();
  167. }
  168. /// <summary>
  169. /// 2号数据源中存在
  170. /// </summary>
  171. public List<T1> Contain_2 { get; set; }
  172. /// <summary>
  173. /// 1号数据源中存在
  174. /// </summary>
  175. public List<T2> Contain_1 { get; set; }
  176. /// <summary>
  177. /// 2号数据源中不存在
  178. /// </summary>
  179. public List<T1> NoContain_2 { get; set; }
  180. /// <summary>
  181. /// 1号数据源中不存在
  182. /// </summary>
  183. public List<T2> NoContain_1 { get; set; }
  184. }
  185. public class View_Property
  186. {
  187. public PropertyInfo Property_1 { get; set; }
  188. public PropertyInfo Property_2 { get; set; }
  189. }
  190. }