List之Distinct()

针对数组可以用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
    } 

posted on   itjeff  阅读(8692)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2014-08-29 转载:C#中的泛型

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示