在C#中的元组列表中找到并删除重复项

编程入门 行业动态 更新时间:2024-10-28 08:25:09
本文介绍了在C#中的元组列表中找到并删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要找到并从一个元组的列表中删除重复项。 基本上,我的结构是由这样的:

I need to find and remove the duplicates from a List of tuples. Basically, my structure is made like that:

List<Tuple<string, string>> myList = new List<Tuple<string, string>>(); **** private void FillStructure() { myList.Add(Tuple.Create<string, string>("A", "B")); myList.Add(Tuple.Create<string, string>("A", "C")); myList.Add(Tuple.Create<string, string>("C", "B")); myList.Add(Tuple.Create<string, string>("C", "B")); // Duplicate myList.Add(Tuple.Create<string, string>("A", "D")); FindAndRemoveDuplicates(myList); } private void FindAndRemoveDuplicates(List<Tuple<string, string>> myList) { // how can I perform this ? }

我不能使用字典,因为我可以有相同的密钥,但不同值! 预先感谢您

I can't use a Dictionary because I can have the same key but different values! Thank you in advance

推荐答案

您可以使用的 鲜明的() LINQ的方法是这样的:

You can use Distinct() method of LINQ, like this:

myList = myList.Distinct().ToList();

请注意,这将重新创建列表,而不是代替删除重复项。

Note that this would re-create the list, rather than removing the duplicates in place.

更多推荐

在C#中的元组列表中找到并删除重复项

本文发布于:2023-11-03 07:34:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!