针对数组可以用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. Codeforces Round #258 (Div. 2) B. Sort the Array

    题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...

  2. Gradle入门系列(转)

    Gradle是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是采用一种基于Groovy的内部领域特定语言.近期,Gradle获得了极大的关注,这也是我决定去研究Gradle的原因. 这篇文章是 ...

  3. jqm视频播放器,html5视频播放器,html5音乐播放器,html5媒体播放器,video开展demo,html5视频播放演示示例,html5移动视频播放器

    最近看到很多有用的论坛html5视频播放的发展,音乐播放功能,大多数都在寻找答案.所以,我在这里做一个demo.对于大家互相学习.html5开发越来越流行,至于这也是一个不可缺少的一部分的视频. 如何 ...

  4. 【UML】概念、关联、画画(一)

    最近画UML画画,于UML观看视频后还没有学会.它是的结果UML九图是不是太懂,我想加深绘制过程的理解,我一个新的水平. 现在我觉得是时候..地介绍一下UML.了解一下它的基本内容.达到深入浅出的效果 ...

  5. 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用

    前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90 ...

  6. maven插件的生命周期的详细说明(两)

    插件配置 定义解释:插件目标 当我们了解了maven插件之后.我们发现假设为每个功能编写一个独立的插件显然是不可取的,由于这些任务背后有非常多能够复用的代码.因此,把这些功能聚集在一个插件里,每个功能 ...

  7. This Android SDK requires Android Developer Toolkit version 22.6.2 or above.

    今天,在android SDK升级时间,我遇到上述错误,经过一番努力仍克服. 解决方法:android-sdk-windows\tools\lib中间plugin.prop在文档 plugin.ver ...

  8. form 为什么上传文件enctype现场

    FORM要素enctype属性指定表单数据server当提交所使用的编码类型,默认默认值它是"application/x-www-form-urlencoded". 这样的编码方式 ...

  9. ZOJ 2412 Farm Irrigation(DFS 条件通讯块)

    意甲冠军  两个农田管内可直接连接到壳体  他们将能够共享一个水源   有11种农田  管道的位置高于一定  一个农田矩阵  问至少须要多少水源 DFS的连通块问题  两个相邻农田的管道能够直接连接的 ...

  10. Redis是新兴的通用存储系统-为何Redis要比Memcached好用

    GitHub版本地址: https://github.com/cncounter/translation/blob/master/tiemao_2014/Redis_beats_Memcached/R ...