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