IList.Distinct() 根据字段过滤重复的数据
2012-04-01 10:00
363 查看
根据网上的例子修改的,记录一下。 IList.Distinct(new Comparint("ID","Name"));
1 /// <summary>
2 /// 根据字段过滤重复的数据
3 /// </summary>
4 /// <typeparam name="T"></typeparam>
5 public class Comparint<T> : IEqualityComparer<T> where T : class, new()
6 {
7 private string[] comparintFiledName = { };
8
9 public Comparint() { }
10 public Comparint(params string[] comparintFiledName)
11 {
12 this.comparintFiledName = comparintFiledName;
13 }
14
15 bool IEqualityComparer<T>.Equals(T x, T y)
16 {
17 if (x == null && y == null)
18 {
19 return false;
20 }
21 if (comparintFiledName.Length == 0)
22 {
23 return x.Equals(y);
24 }
25 bool result = true;
26 var typeX = x.GetType();//获取类型
27 var typeY = y.GetType();
28 foreach (var filedName in comparintFiledName)
29 {
30 var xPropertyInfo = (from p in typeX.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
31 var yPropertyInfo = (from p in typeY.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
32
33 result = result
34 && xPropertyInfo != null && yPropertyInfo != null
35 && xPropertyInfo.GetValue(x, null).ToString().Equals(yPropertyInfo.GetValue(y, null));
36 }
37 return result;
38 }
39 int IEqualityComparer<T>.GetHashCode(T obj)
40 {
41 return obj.ToString().GetHashCode();
42 }
43 }
1 /// <summary>
2 /// 根据字段过滤重复的数据
3 /// </summary>
4 /// <typeparam name="T"></typeparam>
5 public class Comparint<T> : IEqualityComparer<T> where T : class, new()
6 {
7 private string[] comparintFiledName = { };
8
9 public Comparint() { }
10 public Comparint(params string[] comparintFiledName)
11 {
12 this.comparintFiledName = comparintFiledName;
13 }
14
15 bool IEqualityComparer<T>.Equals(T x, T y)
16 {
17 if (x == null && y == null)
18 {
19 return false;
20 }
21 if (comparintFiledName.Length == 0)
22 {
23 return x.Equals(y);
24 }
25 bool result = true;
26 var typeX = x.GetType();//获取类型
27 var typeY = y.GetType();
28 foreach (var filedName in comparintFiledName)
29 {
30 var xPropertyInfo = (from p in typeX.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
31 var yPropertyInfo = (from p in typeY.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
32
33 result = result
34 && xPropertyInfo != null && yPropertyInfo != null
35 && xPropertyInfo.GetValue(x, null).ToString().Equals(yPropertyInfo.GetValue(y, null));
36 }
37 return result;
38 }
39 int IEqualityComparer<T>.GetHashCode(T obj)
40 {
41 return obj.ToString().GetHashCode();
42 }
43 }
相关文章推荐
- List之根据某个字段在add的时候过滤掉重复的数据
- 手动过滤重复字段数据(因为distinct关键字无法使用。。。)
- SQL语句根据表中的几个字段删除重复数据
- SQL语句-统计全部数据,过滤掉其中两个字段同时重复的记录
- Oracle中通过distinct为什么过滤不了重复的数据?
- Oracle 删除重复数据只留一条 查询及删除重复记录的SQL语句 1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 wher
- 用DISTINCT过滤mysql数据库中重复的字段
- php 根据某个字段查询 返回重复数据一条
- Java 之List根据对象字段名去重复
- java去除list中重复的数据【distinct】
- mysql删除数据库表中重复数据(根据单个或多个字段)
- 根据list<object>对象中某个字段去除重复值
- Castle ActiveRecord的distinct过滤重复数据
- MySQL根据某一个或者多个字段查找重复数据
- 检索出 IList<T> 或 List<T>中的不重复数据 Distinct()
- C# datatable过滤某一列的重复数据(相当于distinct)
- 过滤 List 容器中重复(相同)的数据元素
- 按字段过滤重复的数据
- 检索出 IList<T> 或 List<T>中的不重复数据 Distinct()
- LINQ 中使用 Distinct 的 Compare过滤重复的字段