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

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

List<T> intersectList = queryList.AsQueryable().Intersect(sqlList, new ListEquality<T>()).ToList();

   public class ListEquality<T> : IEqualityComparer<T> where T : new()
{ PropertyInfo[] propertys = typeof(T).GetProperties().Where(p => p.Name != "ParamInfo").Where(p => p.Name != "State").ToArray(); public bool Equals(T x, T y)
{
foreach (var pe in propertys)
{
object o_x = pe.GetValue(x, null);
object o_y = pe.GetValue(y, null);
if (!object.Equals(o_x, o_y))
{
return false;
}
}
return true;
} public int GetHashCode(T obj)
{
if (obj == null)
{
return ;
}
else
{
return obj.ToString().GetHashCode();
}
}
}

上面的代码明显是根据排除字段来对比的。如果你不想根据字段对比,那就不要排除。

通过主键属性来对比仅仅是主键的类

    public class ListKeyEquality<T> : IEqualityComparer<T> where T : new()
{ PropertyInfo[] propertys = typeof(T).GetProperties().Where(p => p.Name != "ParamInfo").Where(p => p.Name != "State").ToArray();
public bool Equals(T x, T y)
{
foreach (var pe in propertys)
{
object[] allAttributes = pe.GetCustomAttributes(false);
if (allAttributes.Count() == )
{
continue;
}
if ((allAttributes.FirstOrDefault() as GrantAttribute).IsKey)
{
object o_x = pe.GetValue(x, null);
object o_y = pe.GetValue(y, null);
if (!object.Equals(o_x, o_y))
{
return false;
}
} }
return true;
} public int GetHashCode(T obj)
{
if (obj == null)
{
return ;
}
else
{
return obj.ToString().GetHashCode();
}
}
}
   public class GrantAttribute : Attribute
{
/// <summary>
/// 是否xml类型
/// </summary>
public bool CheckXml { get; set; }
/// <summary>
/// 是否主键
/// </summary>
public bool IsKey { get; set; }
}

类的主键属性调用也是很简单

    public class OrderInfo
{
/// <summary>
/// 本系统订单ID
/// </summary>
[Grant(IsKey = true)]
public Guid OrderId { get; set; }
}

最新文章

  1. JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题
  2. Symantec Backup Exec 2010 Agent For Linux安装
  3. 优化mysql服务器
  4. SQL Server排序规则
  5. sbt assembly build.sbt content
  6. 转:DLL如何导出C++的类
  7. [ActionScript 3.0] AS3 深入理解Flash的安全沙箱Security Domains
  8. 04day2
  9. $_SERVER详细资料整理(转)
  10. Linux中重定向
  11. 初学python列表
  12. 〖Linux〗Linux高级编程 - 进程间通信(Interprocess Communication)
  13. linux 的yum源
  14. 【转载】COM 组件设计与应用(十二)——错误与异常处理
  15. oracle字符串处理函数--待整理
  16. Contest 4
  17. Pygame:编写一个小游戏 标签: pythonpygame游戏 2017-06-20 15:06 103人阅读 评论(0)
  18. Linux:如何获取打开文件和文件描述符数量
  19. 2588: Spoj 10628. Count on a tree
  20. ymal

热门文章

  1. c++并发练习---多线程顺序打印
  2. js设计模式总结-代理模式
  3. Ubuntu 修改 ssh 登录后的欢迎信息
  4. Asp.Net_&lt;%%&gt;模式常用语法
  5. shiro重新赋值权限
  6. linux限制ftp账户的访问路径
  7. Android 写模块化代码注意事项
  8. 使用Kylin构建企业大数据分析平台的4种部署方式
  9. 获取$(this)子节点对象的方法
  10. 删除Kafka的topic