.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

相关推荐
奇怪的杰哥30 分钟前
Win11 加快软件开机自启动
windows
cpsvps43 分钟前
Windows内核并发优化
windows
军训猫猫头4 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
追逐时光者5 小时前
C#/.NET/.NET Core优秀项目和框架2025年6月简报
后端·.net
qq_393828226 小时前
电脑休眠设置
windows·电脑·软件需求
网安小白的进阶之路9 小时前
A模块 系统与网络安全 第三门课 网络通信原理-3
网络·windows·安全·web安全·系统安全
ChaITSimpleLove10 小时前
.NET9 实现斐波那契数列(FibonacciSequence)性能测试
.net·性能测试·斐波那契数列·fibonacci·benchmarkdotnet·datadog.trace
芳草萋萋鹦鹉洲哦16 小时前
【vue3+tauri+rust】如何实现下载文件mac+windows
windows·macos·rust
李洋-蛟龙腾飞公司16 小时前
HarmonyOS NEXT应用元服务常见列表操作多类型列表项场景
windows
new_zhou21 小时前
Windows qt打包编译好的程序
开发语言·windows·qt·打包程序