在C#中的元组列表中找到并删除重复项 [英] Find and Deletes Duplicates in List of Tuples in C#

查看:103
本文介绍了在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#中的元组列表中找到并删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆