List <T> comparison, List <T>
For the comparison of types, the original comparison of linq cannot be distinguished.
Query the intersection of two lists. Intersection. The function of the Union is directly written using Linq. It is not written here.
List <T> intersectList = queryList. AsQueryable (). Intersect (sqlList, new ListEquality <T> (). ToList ();
1 public class ListEquality <T>: IEqualityComparer <T> where T: new () 2 {3 4 PropertyInfo [] propertys = typeof (T ). getProperties (). where (p => p. name! = "ParamInfo"). Where (p => p. Name! = "State "). toArray (); 5 6 7 public bool Equals (T x, T y) 8 {9 foreach (var pe in propertys) 10 {11 object o_x = pe. getValue (x, null); 12 object o_y = pe. getValue (y, null); 13 if (! Object. equals (o_x, o_y) 14 {15 return false; 16} 17} 18 return true; 19} 20 21 public int GetHashCode (T obj) 22 {23 if (obj = null) 24 {25 return 0; 26} 27 else28 {29 return obj. toString (). getHashCode (); 30} 31} 32}View Code
The above code is obviously compared based on excluded fields. If you do not want to compare fields, do not exclude them.
Comparing primary key attributes to primary key-only classes
1 public class listkeypolicity <T>: IEqualityComparer <T> where T: new () 2 {3 4 PropertyInfo [] propertys = typeof (T ). getProperties (). where (p => p. name! = "ParamInfo"). Where (p => p. Name! = "State "). toArray (); 5 public bool Equals (T x, T y) 6 {7 foreach (var pe in propertys) 8 {9 object [] allAttributes = pe. getCustomAttributes (false); 10 if (allAttributes. count () = 0) 11 {12 continue; 13} 14 if (allAttributes. firstOrDefault () as GrantAttribute ). isKey) 15 {16 object o_x = pe. getValue (x, null); 17 object o_y = pe. getValue (y, null); 18 if (! Object. equals (o_x, o_y) 19 {20 return false; 21} 22} 23 24} 25 return true; 26} 27 28 public int GetHashCode (T obj) 29 {30 if (obj = null) 31 {32 return 0; 33} 34 else35 {36 return obj. toString (). getHashCode (); 37} 38} 39}View Code 1 public class GrantAttribute: Attribute 2 {3 /// <summary> 4 /// whether the xml type is 5 /// </summary> 6 public bool CheckXml {get; set;} 7 // <summary> 8 // whether the primary key is 9 // </summary> 10 public bool IsKey {get; set;} 11}View Code
Class primary key property call is also very simple
Public class OrderInfo {// <summary> /// order ID of the current system /// </summary> [Grant (IsKey = true)] public Guid OrderId {get; set ;}}View Code