针对数组可以用List.Distinct(),可以过滤掉重复的内容。

针对对象中的某个字段只能用Distinct(IEqualityComparer<T>)

用法:


 1  public class AppIndex:BasePage
 2     {
 3         public void DoGet()
 4         {
 5             List<test11> list_test = new List<test11>();
 6             list_test.Add(new test11() { 
 7             m=1,
 8             v="one"
 9             });
10             list_test.Add(new test11()
11             {
12                 m = 2,
13                 v = "two"
14             });
15             list_test.Add(new test11()
16             {
17                 m = 3,
18                 v = "three"
19             });
20             list_test.Add(new test11()
21             {
22                 m = 4,
23                 v = "fornt"
24             });
25              list_test.Add(new test11()
26             {
27                 m = 4,
28                 v = "fornt"
29             });
30               list_test.Add(new test11()
31             {
32                 m = 3,
33                 v = "fornt"
34             });
35               var ss = list_test.Distinct(new Comparint());//这里调用
36             this.Add("mylist",new Travel.DAL.AppActive().GetList(BaseCode));
37         }
38            }
39 
40    public class test11
41    {
42        public int m { get; set; }
43        public string v { get; set; }
44    }
45    public class Comparint : IEqualityComparer<test11>
46    {
47 
48        public bool Equals(test11 x, test11 y)
49        {
50            if (x == null && y == null)
51                return false;
52            return x.m==y.m;
53        }
54 
55        public int GetHashCode(test11 obj) {
56            return obj.ToString().GetHashCode();
57        }
58    }
 

同样table 也可以用这个方法。只要一步ASEnumerable()即可

            var _comPresult = _dt.AsEnumerable().Distinct(new DataTableRowCompare());
            DataTable _resultDt = _comPresult.CopyToDataTable();
            _resultDt.AsEnumerable().ToList().ForEach(
               x =>
               {
                   Console.WriteLine(x["id"].ToString() + "    " + x["name"].ToString() + "   " + x["address"].ToString());
               });
 
 
public class DataTableRowCompare : IEqualityComparer<DataRow>
    {
 
        #region IEqualityComparer<DataRow> 成员
 
        public bool Equals(DataRow x, DataRow y)
        {
            return (x.Field<int>("id") == y.Field<int>("id")); 这个是根据自己的需求写比较字段的
        }
 
        public int GetHashCode(DataRow obj)
        {
            return obj.ToString().GetHashCode();
        }
 
        #endregion
    } 

List之Distinct()的更多相关文章

  1. [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  2. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  3. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  4. SQL中distinct的用法

    SQL中distinct的用法   1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...

  5. Oracle 查询语句(where,order by ,like,in,distinct)

    select * from production;alter table production add productionprice number(7,2); UPDATE production s ...

  6. mysql中distinct的用法

    本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...

  7. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  8. distinct 与 group by 去重

    例如下表格:表名:fruit id Name Price Num 1 西瓜 10 2 2 西瓜 11 2 3 香蕉 10 3 4 桃子 10 2 当我想获取Name不重复的数据,结果如下 id Nam ...

  9. 大数据下的Distinct Count(二):Bitmap篇

    在前一篇中介绍了使用API做Distinct Count,但是精确计算的API都较慢,那有没有能更快的优化解决方案呢? 1. Bitmap介绍 <编程珠玑>上是这样介绍bitmap的: B ...

  10. 扩展lamda表达中distinct按照字段去除重复

    首先,我们定义一个Student类来测试. public class Student { public int ID { get; set; } public string Name { get; s ...

随机推荐

  1. CLR Profile解决内存占用过高

    CLR Profile解决内存占用过高的问题 炮哥:"嘿,哥们,忙啥呢,电脑卡成这逼样." 勇哥:"在用CLR Profile工具分析下FlexiPrint的内存占用情况 ...

  2. Ubuntu在构建Robotframework+Selenium周围环境

    最近经历了从Windows进入系统Ubuntukylin下列.因此,测试工具也需要被重新安装,今天和共享安装过程. 我用的是环境:Ubuntu Kylin 14.04 64Bit系统. 启动权,首先, ...

  3. 经常使用Log日志打印输出

    /** * log刊物 * @author Jenly * */ public class LogUtils { public static final String TAG = "Jenl ...

  4. PHP关联数组和哈希表(hash table) 未指定

    PHP有数据的一个非常重要的一类,就是关联数组.又称为哈希表(hash table),是一种很好用的数据结构. 在程序中.我们可能会遇到须要消重的问题,举一个最简单的模型: 有一份username列表 ...

  5. docker-gitlab(转)

    Issues Docker is a relatively new project and is active being developed and tested by a thriving com ...

  6. SDUTOJ 1298 活动选择

    #include<iostream> #include<memory.h> using namespace std; int a[105],b[105],c[105],d[10 ...

  7. Win10使用中的一些问题

    闲来无事,怒装Win10.使用上总体来说还是不错的,比Win8好一个档次吧. 不过呢在使用中遇到两个很郁闷的问题.权且几下 1.Win10激活 使用工具:激活工具 2.激活后浏览器被挟持 这让我现在非 ...

  8. ext Window点击右上角的关闭(Xbutton)加入监控事件

    使用场景:关闭window的时候添加监听事件. 正确的使用方式: addwin = new Ext.Window({ title : '新增', closable : true, width : 50 ...

  9. java中三种常见内存溢出错误的处理方法(good)

    相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的认识. 在解决j ...

  10. Iterator、Iterable接口的使用及详解

    Java集合类库将集合的接口与实现分离.同样的接口,可以有不同的实现. Java集合类的基本接口是Collection接口.而Collection接口必须实现Iterator接口. 以下图表示集合框架 ...