.net-去重的几种情况

文章目录

  • 前言
      • [1. int 类型的list 去重](#1. int 类型的list 去重)
      • [2. string类型的 list 去重](#2. string类型的 list 去重)
      • [3. T泛型 List去重](#3. T泛型 List去重)
      • [4. 使用HashSet List去重](#4. 使用HashSet List去重)
      • [5. 创建静态扩展方法](#5. 创建静态扩展方法)
  • 总结

前言

.net 去重的几种情况


1. int 类型的list 去重

js 复制代码
//  List<int> 
  List<int> myList = new List<int>(){ 100 , 200 ,100 ,300};
        myList  = myList.Distinct().ToList();
        foreach (var item in myList)
        {
            Console.WriteLine(item); // 100 200 300
        }

2. string类型的 list 去重

js 复制代码
 List<string> myList = new List<string>(){ "100" , "200" ,"300" ,"300"};
        myList  = myList.Distinct().ToList();
        foreach (var item in myList)
        {
            Console.WriteLine(item);
        }

3. T泛型 List去重

js 复制代码
 public class User
 {
   public int Id { get; set; }
   public string Name { get; set; }
   public int Age { get; set;}
 }

 List<User> myList1= new List<User>() 
 { 
    new User() { Id = 1, Name = "张三", Age = 11 } ,
    new User() { Id = 1, Name = "张三", Age = 11} ,
    new User() { Id = 3, Name = "李四", Age = 13 } ,
 };
 // groupBy 方式
 myList1= myList1.GroupBy(p => p.Id).Select(q => q.First()).ToList();
 foreach (var item in myList1)
 {
   Console.WriteLine("Id:" + item.Id + ", Name:" + item.Name + ", Age:" + item.Age);
 }
 
 //  key 方式如下图
 var myList2 = (from p in myList1
              group p by new { p.Id, p.Name, p.Age } into g
              select g).ToList();
 foreach (var item in myList2 )
 {
    Console.WriteLine("Id:" + item.Key.Id + ", Name:" + item.Key.Name + ", Age:" + item.Key.Age);
 }

4. 使用HashSet List去重

js 复制代码
List<int> list = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
        HashSet<int> set = new HashSet<int>(list);
        List<int> distinctList = set.ToList();

5. 创建静态扩展方法

js 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var people = new List<Person>
            {
                new Person {Id=1,Name="Curry",Age=26 },
                new Person {Id=1,Name="Curry",Age=26 },
                new Person {Id=3,Name="James",Age=27 },
                new Person {Id=4,Name="Kobe",Age=38 }
            };
            var distinctPeople = people.DistinctBy(x => x.Name).ToList();
            distinctPeople.ForEach(x =>
            Console.WriteLine($"Id:{x.Id},Name:{x.Name},Age:{x.Age}")
            );
            Console.ReadKey();
        }
    }
 
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
    // 创建静态扩展方法
    public static class DistinctHelper
    {
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            var identifiedKeys = new HashSet<TKey>();
            return source.Where(element => identifiedKeys.Add(keySelector(element)));
        }
    }
}

总结

本文探讨了在.net 几种常用类型数组的去重方式,关键用到 Distinct(),GroupBy(), HashSet。

参考:

https://blog.csdn.net/laizhixue/article/details/89228355

相关推荐
步、步、为营29 分钟前
.NET 8 Release Candidate 1 (RC1)现已发布,包括许多针对ASP.NET Core的重要改进!
r语言·asp.net·.net
joe023510 小时前
电脑安装 Win10 提示无法在当前分区上安装Windows的解决办法
windows·gpt·电脑·uefi
前端 贾公子10 小时前
vue-cli 模式下安装 uni-ui
前端·javascript·windows
斯是 陋室11 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
Elastic 中国社区官方博客12 小时前
在 Windows 上使用 Docker 运行 Elastic Open Crawler
大数据·windows·爬虫·elasticsearch·搜索引擎·docker·容器
CIAS15 小时前
clonezilla 导出自动化恢复iso
linux·windows·clonezilla
墨菲安全15 小时前
Node.js Windows下路径遍历漏洞
windows·node.js·路径遍历漏洞
NoirSeeker16 小时前
在windows平台上基于OpenHarmony sdk编译三方库并暴露给ArkTS使用(详细)
c++·windows·arkts·鸿蒙·交叉编译
红藕香残玉簟秋17 小时前
【python学习】windows使用conda管理python虚拟环境
windows·python·conda
程序视点18 小时前
Microsoft .Net 运行库离线合集包专业解析
前端·后端·.net