惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
Google DeepMind News
Google DeepMind News
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
T
The Blog of Author Tim Ferriss
D
Docker
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Know Your Adversary
Know Your Adversary
A
About on SuperTechFans
U
Unit 42
NISL@THU
NISL@THU
M
MIT News - Artificial intelligence
T
The Exploit Database - CXSecurity.com
K
Kaspersky official blog
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hacker News - Newest:
Hacker News - Newest: "LLM"
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
WordPress大学
WordPress大学
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
小众软件
小众软件
G
Google Developers Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园 - 司徒正美
腾讯CDC
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Security @ Cisco Blogs
aimingoo的专栏
aimingoo的专栏
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
N
News and Events Feed by Topic
Google Online Security Blog
Google Online Security Blog
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog

博客园 - 宏宇

C#中一个简单的Task C#中多线程Task详解 Automa – AI浏览器扩展工具 超好用的网页端自动化扩展工具-Automa HTTP 请求“报头”——Referer 和 Cookie 使用vs2022将.net8的应用程序发布为一个单独文件 Thread.Abort的.Net Core替代方法 元数据、数据元、数据源、源数据 IIS设置使某IP访问时,跳转到指定页面 IIS自定义错误页面 关闭PerfWatson2.exe 体验改善计划 C# 线程(Thread) 配置 sql server 最大内存 sqlserver内存最佳配置 Windows安装时调出系统的cmd功能 Shift+F10 windows无法安装到这个磁盘,选中的磁盘采用gpt分区 如何删除硬盘efi系统分区 site命令 win10系统图片打开方式为照片查看器的方法 不安装运行时运行.NET程序
C#数据去重的5种方式
宏宇 · 2025-03-19 · via 博客园 - 宏宇

前言

今天我们一起来讨论一下关于C#数据去重的的5种方式,每种方法都有其特点和适用场景,我们根据具体需求选择最合适的方式。当然欢迎你在评论区留下你觉得更好的C#数据去重的方式。

使用HashSet去重

C# 中的 HashSet 是一种集合类型,它确保其中的元素是唯一的,不允许重复值的存在。当你尝试向 HashSet 中添加一个重复的元素时,HashSet 会忽略重复的值,而不会引发错误。这使得 HashSet 成为一个非常方便的数据结构,用于存储一组唯一的元素,并且在需要时可以高效地进行查找、插入和删除操作,注意HashSet中的元素是无序的。

        /// <summary>
        /// 使用HashSet去重
        

使用Linq的Distinct()方法去重

Linq中的Distinct()方法用于从集合中筛选出不重复的元素。Distinct()方法基于元素的相等性来进行筛选,并返回一个包含不重复元素的新序列。底层实现还是使用到了HashSet。

        /// <summary>
        /// 使用Linq的Distinct()方法去重
        

使用Linq的GroupBy()方法去重

GroupBy()方法将原始集合中的元素进行分组,根据指定的键或条件进行分组。每个分组都会有一个唯一的键,通过将原始集合分组并选择每个分组中的第一个元素,实现了去重的效果。

        /// <summary>
        /// 使用Linq的GroupBy()方法去重
        

如果多字段去重

            models = models.GroupBy(p => new { p.Value1, p.Value2 }).Select(p => p.FirstOrDefault()).ToList();//分组去重

使用自定义的比较器和循环遍历

    public class ArrayDeduplication
    {
        /// <summary>
        

直接循环遍历去重

        /// <summary>
        /// 直接循环遍历去重