针对数组可以用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. C++笔试面试总结

    手游广州某公司书面今天接受采访时.刚进去中午1中场休息.他们公司谁刚刚醒来,一个冷漠打牌,然后去上班.瞬间,这些公司有没有什么好印象,压抑. 接着快2点的时候.发了一份笔试题.大部分题目均在网上的&l ...

  2. hdu 4944 FSF’s game(数论)

    题目链接:hdu 4944 FSF's game 题目大意:给定N,能够用不大于N的长a和宽b.组成N∗(N−1)2种不同的矩形,对于每一个矩形a∗b要计算它的值,K为矩形a,b能够拆分成若干个K∗K ...

  3. Python爬虫(一)

    花了四天的时间用python写了个简单的爬虫程序.整个过程分为两个部分:工具的安装和程序的实现 本文并没有讲程序的详细实现遇到的问题,而是对着手前一些前期的准备 第一部分(工具的安装) 开发工具的下载 ...

  4. 【Linux探索之旅】第二部分第一课:终端Terminal,好戏上场

    内容简介 1.第二部分第一课:终端Terminal,好戏上场 2.第二部分第二课预告:命令行,世界尽在掌握 终端Terminal,好戏上场 随着第一部分的结束,我们进入了第二部分(小编你这好像是废话. ...

  5. 认识Backbone (二)

    Backbone.Model(模型) Models(模型)是任何Javascript应用的核心,包括数据交互及与其相关的大量逻辑: 转换.验证.计算属性和访问控制.Model在Backbone中为数据 ...

  6. 如何区分MNO和MVNO

    MVNO(Mobile Virtaul Network Operator)虚拟网络运营商,没有自己的物理网络,租用MNO(Mobile Network Operator)网络提供的网络服务. 我们知道 ...

  7. 解决 U盘安装Windows Server 2012 R2 报错 Windows 无法打开所需的文件 Sources\install.wim

    报错原因: 使用UltraISO等软件刻录镜像时默认使用FAT32文件系统,该系统不支持大于4G的文件, 而Server 2012 R2的安装文件install.wim为5.12G,固安装失败. 解决 ...

  8. ruby简单的基本 3

    类 Ruby一切都是对象,它包含了一个恒定.例如,可以使用.class物业查看对象的类型,你可以看一下1.class.你会发现常1类型是Fixnum,1但它是Fixnum的一个例子. Ruby本类cl ...

  9. Kotlin

    关于Kotlin,网上已有一些介绍的文章,包括Antonio Leiva的这组blog翻译稿.不过,我还是想跟进它们.翻译它们,以锻炼自己的英文翻译.各位高手发现问题,请及时“拍砖”. 原文题目:Ko ...

  10. hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分法)

    Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...