针对数组可以用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. 9种CSS3 blend模式制作的鼠标滑过图片标题特效

    这是一款使用CSS3 background-blend-mode制作的鼠标滑过图片标题特效.该图片标题特效在鼠标滑过一张图片的时候,图片的标题会对应的动画,而且图片会使用css blend模式渲染为很 ...

  2. Android 设计模式模式适配器

    自定义适配器模式:一类的接口,转换成客户的期望,也是一个接口.适配器使原本接口不是与类兼容可以无缝.下面两个图看起来更加清晰 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  3. 从控制台读取password - C#

    Tip :    从控制台读取password 语言: C# ______________________________________________________________ 在登陆Lin ...

  4. HDU 1228 A + B 浙江大学研究生冠军

    Problem Description 读入两个小于100的正整数A和B,计算A+B. 须要注意的是:A和B的每一位数字由相应的英文单词给出.   Input 測试输入包括若干測试用例,每一个測试用例 ...

  5. UVA580-Critical Mass

    题目链接 题意:一个栈中仅仅能放入U和L,问存在连续3个以上U(危急组合)的个数为几个 思路:用总组合数-安全组合=危急组合.d[i]表示第i个位置以L结束的序列,所以就有d[i] = d[i - 1 ...

  6. redis源代码解读之内存管理————zmalloc文件

    本文章主要记录本人在看redis源代码的一些理解和想法.由于功力有限,肯定会出现故障,所以.希望高手给出指正. 第一篇就是内存相关的介绍.由于我喜欢先看一些组件的东西,再看总体的流程. 先上一下代码吧 ...

  7. VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性

    本文将创建一个简单的动态库-link,谱写控制台应用程序使用该动态链接库,该动态链接库为"JAVA调用动态链接库DLL之JNative学习"中使用的DLL,仅仅是项目及文件名不同. ...

  8. OpenGL学习日记-2015.3.13——多实例渲染

        实例化(instancing)或者多实例渲染(instancd rendering)是一种连续运行多条同样渲染命令的方法.而且每一个命令的所产生的渲染结果都会有轻微的差异. 是一种很有效的.有 ...

  9. Java-如何去掉JFrame上的最大化最小化和关闭按钮(转)

    在JDK1.4以前,我们只有一种方式来去掉窗口的标题栏,那就是直接使用JWindow,用JWindow来代替JFrame使用.但用过JWindow的人一定知道,JWindow在操作系统的任务栏是不可见 ...

  10. Cocos2d-x 3.0 Lua编程 之 响应物理引擎的Contact事件回调不运行的问题

    在较早的版本号如3.0beta使用例如以下代码的话: -- add ground local groudNode = cc.Node:create() groudNode:setPhysicsBody ...