• 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. 【Springboot】Springboot整合Jasypt,让配置信息安全最优雅方便的方式

    1 简介 在上一篇文章中,介绍了Jasypt及其用法,具体细节可以查看[Java库]如何使用优秀的加密库Jasypt来保护你的敏感信息?.如此利器,用之得当,那将事半功倍.本文将介绍Springboo ...

  2. vuex模块化。

    项目结构: 1:在src下新建目录store,然后再建storemodule.js文件,把 上篇 store.js文件抽出来: import Vue from 'vue' import Vuex fr ...

  3. LightOJ 1344 Aladdin and the Game of Bracelets

    It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a ...

  4. UVA-11107 Life Forms(求出现K次的子串,后缀数组+二分答案)

    题解: 题意: 输入n个DNA序列,你的任务是求出一个长度最大的字符串,使得它在超过一半的DNA序列中出现.如果有多解,按照字典序从小到大输入所有解. 把n个DNA序列拼在一起,中间用没有出现过的字符 ...

  5. STM32调试总结

    1.卡死在这里的问题:没有中断处理函数,程序无法进入中断处理函数.DMA2_Channel3_IRQHandlerDMA2_Channel4_IRQHandlerDMA2_Channel5_IRQHa ...

  6. 201871010119-帖佼佼《面向对象程序设计(java)》第十六周学习总结

    博文正文开头格式:(2分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.co ...

  7. nginx配置隐藏index.php

    Nginx 服务器隐藏 index.php 配置   location / { try_files $uri $uri/ /index.php?$query_string; }   nginx配置中t ...

  8. SQL中Group By和having的用法

    转自 ITGirl笑笑   一.GROUP BY GROUP BY语句用来与聚合函数(aggregate functions such as COUNT, SUM, AVG, MIN, or MAX. ...

  9. css应用视觉设计

    应用视觉设计:创建一个 CSS 线性渐变 HTML元素的背景色并不局限于单色.css还提供了颜色过渡,也就是渐变.可以通过background里面的linear-gradient()来实现线性渐变,下 ...

  10. vue-cli4.0脚手架安装

    10月16日,官方发布消息称Vue-cli 4.0正式版发布,并且强烈建议升级;小编也是刚刚安装了最新版本的脚手架看看更新了什么 安装和vue-cli3.0的是一模一样的,对比了一下3.0的脚手架,除 ...