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


前言

.net 去重的几种情况


1. int 类型list 去重

//  List<int&gt; 
  List<int&gt; myList = new List<int&gt;(){ 100 , 200 ,100 ,300};
        myList  = myList.Distinct().ToList();
        foreach (var item in myList)
        {
            Console.WriteLine(item); // 100 200 300
        }

2. string类型list 去重

 List<string&gt; myList = new List<string&gt;(){ "100" , "200" ,"300" ,"300"};
        myList  = myList.Distinct().ToList();
        foreach (var item in myList)
        {
            Console.WriteLine(item);
        }

3. T泛型 List去重

 public class User
 {
   public int Id { get; set; }
   public string Name { get; set; }
   public int Age { get; set;}
 }

 List<User&gt; myList1= new List<User&gt;() 
 { 
    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去重

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. 创建静态扩展方法

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

原文地址:https://blog.csdn.net/Bruce__taotao/article/details/134718467

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_18635.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注