C# Distinct去重泛型List

  • 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);
 }

原文地址:https://www.cnblogs.com/LuckyZLi/p/11888239.html

时间: 2024-08-02 11:40:03

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

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

关于Django中的数据库操作API之distinct去重的一个误传 最近在做一个Server的项目,后台框架是Apache mod_wsgi + django.django是一个基于Python的Web开发框架,功能十分强大,至于有多强大,还是读者们自己去体验吧.我在这里要说的一个问题是关于Python的ORM功能的.问题就在django提供的数据库操作API中的distinct()函数,了解SQL语句的读者都应该知道,DISTINCT关键字可以在select操作时去重.django里的这个d

.net Distinct 去重问题分析

问题:.net中的distinct方法对于自定义的类(class model )去重时失效.问题分析: model1: 1 public class TestModel1 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 public string Address { get; set; } 6 } model2: public class TestModel2 { protected bool Equa

List&lt;object&gt;进行Distinct()去重

原文:List<object>进行Distinct()去重 有时我们会对一个list<T>集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来.如果list<T>中的T是个自定义对象时直接对集合Distinct是达不到去重的效果.我们需要新定义一个去重的类并继承IEqualityComparer接口重写Equals和GetHashCode方法,如下Demo 1 using System; 2 using System.Collections.Ge

SQLSever 第二堂课,主要学习内容为top查询前多少行,distinct去重,order by排序,group by分组,最重要子查询

create database xuesheng drop database xusheng use xuesheng go create table xueshengxinxi ( code int primary key identity(1,1)not null, name varchar(50) not null, age int not null, sex varchar(50) not null, hight int not null, [weight] int not null,

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

1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ           ( bzj, xkzid,  sqid, jtdz, szsf, szqx, szjd, lxdh, yb, instnum, lastdate, datexc)     select  SYS_GUID(), new_xkz_idIn, a.* from    (select distinct   sqid, jtdz, szsf, szqx, szjd, l

in和exists的区别以及exists和distinct去重的区别?

小编相信大家都知道in和exists的区别:1.运用情况不同sql中in适用于子查询得出的结果集记录较少,主查询中的表较大且又有索引的表,.sql中exist适用于外层的主查询记录较少,子查询中的表大,又有索引的时候. 2.驱动顺序不同IN是先查询子查询的表,然后将内表和外表做一个笛卡尔积,然后按照条件进行筛选.exists是以外层表为驱动表,先被访问. 3.底层原理不同in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询. 但是我昨天看到

单表查询: where group by 分组 having distinct 去重 order by 排序 limit 多表查询 子查询 连表查询

今日内容 表查询 单表查询: where group by 分组 having distinct 去重 order by 排序 limit 多表查询 子查询 连表查询 单表查询 前期表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex enum('male','female') not null default 'male', #大部分是男的 age int(3) u

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

进入正题,准备我们的测试数据 1.我们要筛选的数据为去除 GX 列的重复项 并将所有数据展示出来,如图所示: 1 select t.* from [PeopleCount] as t where t.procedureID='8334' 2.这种情况下我们是不可以使用DISTINCT来去重的,我们可以来尝试一下: 首先,单纯的查询 GX 这一列用 distinct 是没有任何问题的 1 select distinct t.GX from [PeopleCount] as t where t.pr

lambda表达式的distinct去重的坑坑坑坑坑

天真的我最开始以为可以写成list.distinct(x=>x.name);以为这样就可以按照name去重了,结果是不行的.这里记录下正确的用法. 1.这里是针对int集合  可以满足 #region 对数字集合的去重 //List<int> list = new List<int> { 1,1,2,3,4,5,5,6,6,7,7,7 }; //foreach (var item in list) //{ // Console.Write(item+" "