根据字段过滤重复数据
using System.Collections.Generic;using System.Linq;namespace KanChang.Util{ ////// 版 本 1.0.0.0 /// Copyright (c) 2012-2017 张刊 /// CLR版本:4.0.30319.42000 /// 创建人:张刊 (kan.zhang-cn@hotmail.com) /// 描 述:可以根据字段过滤重复的数据 /// public class Comparint: IEqualityComparer where T : class, new() { private string[] comparintFiledName = { }; public Comparint() { } public Comparint(params string[] comparintFiledName) { this.comparintFiledName = comparintFiledName; } bool IEqualityComparer .Equals(T x, T y) { if (x == null && y == null) { return false; } if (comparintFiledName.Length == 0) { return x.Equals(y); } bool result = true; var typeX = x.GetType();//获取类型 var typeY = y.GetType(); foreach (var filedName in comparintFiledName) { var xPropertyInfo = (from p in typeX.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault(); var yPropertyInfo = (from p in typeY.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault(); result = result && xPropertyInfo != null && yPropertyInfo != null && xPropertyInfo.GetValue(x, null).ToString().Equals(yPropertyInfo.GetValue(y, null)); } return result; } int IEqualityComparer .GetHashCode(T obj) { return obj.ToString().GetHashCode(); } }}