• List<int>去重
  • List<string>去重
  • List<T>去重

1. List<int>去重

 List<int> ilist = new List<int>() { 1, 2, 3, 4, 2, 3 };
ilist = ilist.Distinct().ToList();
foreach (var item in ilist)
{
Console.WriteLine(item);
}

2. List<string>去重

 List<string> strList = new List<string>() { "4", "4", "5", "6", "6" };
strList = strList.Distinct().ToList();
foreach (var item in strList)
{
Console.WriteLine(item);
}

3. List<T>去重

 public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set;}
} //Main方法
List<User> list = new List<User>()
{
new User() { Id = 1, Name = "张三", Age = 11 } ,
new User() { Id = 1, Name = "张三", Age = 11} ,
new User() { Id = 3, Name = "李四", Age = 13 } ,
};
//方法一
list = list.GroupBy(p => p.Id).Select(q => q.First()).ToList();
//方法二
list = list.GroupBy(p => p.Name).Select(q => q.First()).ToList();
//方法三
list = list.GroupBy(p => p.Age).Select(q => q.First()).ToList();
foreach (var item in list)
{
Console.WriteLine("Id:" + item.Id + ", Name:" + item.Name + ", Age:" + item.Age);
}
//方法四
var list1 = (from p in list
group p by new { p.Id, p.Name, p.Age } into g
select g).ToList();
foreach (var item in list1)
{
Console.WriteLine("Id:" + item.Key.Id + ", Name:" + item.Key.Name + ", Age:" + item.Key.Age);
}

C# Distinct去重泛型List的更多相关文章

  1. Linq 中的distinct去重

    Linq的Distinct和T-Sql的distinct一样,可以将重复的结果集去重注意: 1 distinct去重记录要求每个字段都重复时,才算重复对象,这与sql一样2 distinct语句可以和 ...

  2. 存储过程系列三:根据表别名方式distinct去重插入

    1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ           ( bzj, xkzid,  sqid, jtdz, szsf, ...

  3. .NET-list扩展方法Distinct去重

    原文链接:https://blog.csdn.net/daigualu/article/details/70800012 .NET中list的扩展方法Distinct可以去掉重复的元素,分别总结默认去 ...

  4. .Net Collection Distinct 去重

    由于业务场景的需要,海量的数据需要进行处理.组装,难免会出现冗余的重复数据.如何处理重复的数据就是一个问题. 简单的集合中,去重就可以用linq distinct来完成.对于复杂的集合直接使用dist ...

  5. postgresql中使用distinct去重

    select语法 [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ...

  6. List<object>进行Distinct()去重

    有时我们会对一个list<T>集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来.如果list<T>中的T是个自定义对象时直接对集合Distinct是 ...

  7. DISTINCT 去重仍有重复的分析

    logger日志报错 插入数据时违反主键唯一约束 org.springframework.dao.DuplicateKeyException: ### Error updating database. ...

  8. 关于Django中的数据库操作API之distinct去重的一个误传

    转载自http://www.360doc.com/content/18/0731/18/58287567_774731201.shtml django提供的数据库操作API中的distinct()函数 ...

  9. SQLSERVER去除某一列的重复值并显示所有数据\DISTINCT去重\ISNULL()求SUM()\NOT EXISTS的使用

    进入正题,准备我们的测试数据 1.我们要筛选的数据为去除 GX 列的重复项 并将所有数据展示出来,如图所示: ' 2.这种情况下我们是不可以使用DISTINCT来去重的,我们可以来尝试一下: 首先,单 ...

随机推荐

  1. [TimLinux] 开博一个月了

    做事情,怕的确实是坚持!为了自己尽可能的每天一篇博文,时常在下班后,23点开始写博,有时候写到接近一点钟.但是这第一个月,确实坚持下来了.平均每天一篇.写博的过程,其实是: 知识的总结过程: 因为要求 ...

  2. python获取bing地图发布自己的TMS服务(一)下载瓦片

    部分结果 bing地图瓦片使用QuadKey作为命名方式. QuadKey简介 如何计算quadkey 在给定level下,把行号tileY和列号tileX转换为2进制,然后行列交叉存储,再转换为4进 ...

  3. BZOJ3110[Zjoi2013]K大数查询(树状数组+整体二分)

    3110 [Zjoi2013]K大数查询 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a ...

  4. 2017 ACM/ICPC 沈阳 F题 Heron and his triangle

    A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...

  5. CodeForces 607B zuma

    Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the ...

  6. harbor部署常见的错误

    总结部署harbor过程所遇到的一些坑   1:在使用docker push镜像的时候提示:denied: requested access to the resource is denied,用户和 ...

  7. 表格数据js初始绑定

    html调用js文件,js初始化时发送Ajax请求,获取页面数据将其写入在html页面上展示 html <div class="card-body"> <!-- ...

  8. 更新Preloader和uboot

    在SoCEDS环境下编译和更新preloader和uboot程序的方法   前面有介绍preloader在HPS boot过程中的的作用,接下来讲述下用户在SoCEDS环境下改如何编译preloade ...

  9. webpack学习_资源管理(loader)

    webpack 最出色的功能之一就是,除了 JavaScript,还可以通过 loader 引入任何其他类型的文件 引入资源步骤 Step1:安装你需要的loader  Step2:在 module配 ...

  10. CCF-CSP题解 201509-4 高速公路

    有点忧愁.\(CSP\)也考\(Tarjan\)缩点的嘛. 原理咱也不明白,咱也不敢学,找到模板就是抄. #include<bits/stdc++.h> const int maxn = ...