对于类型的对比,linq原来的对比是区分不了的。

对两个list进行查询交集方法。交集,并集的函数是直接用Linq的,这里不再写。

 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             else
28             {
29                 return obj.ToString().GetHashCode();
30             }
31         }
32     }
View Code

相关文章:

  • 2021-12-09
  • 2022-12-23
  • 2022-01-12
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2021-05-29
猜你喜欢
  • 2022-03-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2021-06-19
相关资源
相似解决方案