如何删除excel范围内的重复项? C#

我已经将string转换为excel范围内的单元格,形成一个string列表,并在原始列表中以逗号分隔每个项目。 我开始认为我没有真正分离每个项目,他们仍然是一个整体,试图找出如何正确地做到这一点,使每个项目(即the_red_bucket_01)是它自己的string。

单元格1和2中原始string的示例:

单元格1:

the_red_bucket_01, the_blue_duck_01,_the green_banana_02, the orange_bear_01 

单元格2:

 the_purple_chair_01, the_blue_coyote_01,_the green_banana_02, the orange_bear_01 

新的列表看起来像这样,但我不知道他们是单独的项目:

 the_red_bucket_01 the_blue_duck_01 the green_banana_02 the orange_bear_01 the_red_chair_01 the_blue_coyote_01 the green_banana_02 the orange_bear_01 

现在,我想删除重复项,以便控制台只显示每个项目的1,无论它们有多less,我似乎无法让我的foreah / if语句正常工作。 它打印出多个项目的副本,我假设,因为它正在迭代列表中的每个项目,所以它是返回多个项目的数据。

  foreach (Excel.Range item in xlRng) { string itemString = (string)item.Text; List<String> fn = new List<String>(itemString.Split(',')); List<string> newList = new List<string>(); foreach (string s in fn) if (!newList.Contains(s)) { newList.Add(s); } foreach (string combo in newList) { Console.Write(combo); } 

您可能需要修剪string,因为它们具有前导空格,所以“string1”与“string1”不同。

 foreach (string s in fn) if (!newList.Contains(s.Trim())) { newList.Add(s); } 

通过使用Distinct,您可以使用Linq更简单。

通过使用默认的相等比较器来比较值,从一个序列中返回不同的元素。

 foreach (Excel.Range item in xlRng) { string itemString = (string)item.Text; List<String> fn = new List<String>(itemString.Split(',')); foreach (string combo in fn.Distinct()) { Console.Write(combo); } } 

正如在另一个答案中提到的,您可能还需要Trim任何空格,在这种情况下,您可以这样做:

 fn.Select(x => x.Trim()).Distinct() 

在需要包含键/值的地方,最好使用Dictionarytypes。 尝试使用List<T>更改为Dictionary<T> 。 即来自:

 List<string> newList = new List<string>(); foreach (string s in fn) if (!newList.Containss)) { newList.Add(s); } 

 Dictionary<string, string> newList = new Dictionary<string, string>(); foreach (string s in fn) if (!newList.ContainsKey(s)) { newList.Add(s, s); } 

如果您在阅读时关注不同的项目,则只需使用Distinct运算符(如fn.Distinct()

为了处理整个数据,我可以提出两种方法:

  1. 读入整个数据,然后使用LINQ的Distinct运算符

  2. 或者使用Set数据结构,并在读取excel的同时存储每个元素

如果你正在处理数据,我build议你看一下LINQ文档。 它有非常好的扩展。 对于更多的方法,你可以看看MoreLINQ包。

我认为你的代码可能会像你期望的那样工作,如果你把newList从循环中移出 – 你在每个循环中创build一个名为newList的新variables,所以它不会从早期的循环中find重复的东西。

您可以使用Linq更简洁地完成所有这些工作:

 //set up some similar data string list1 = "a,b,c,d,a,f"; string list2 = "a,b,c,d,a,f"; List<string> lists = new List<string> {list1,list2}; // find unique items var result = lists.SelectMany(i=>i.Split(',')).Distinct().ToList(); 

SelectMany()将列表的列表“变平”到列表中。

Distinct()删除重复项。

 var uniqueItems = new HashSet<string>(); foreach (Excel.Range cell in xlRng) { var cellText = (string)cell.Text; foreach (var item in cellText.Split(',').Select(s => s.Trim())) { uniqueItems.Add(item); } } foreach (var item in uniqueItems) { Console.WriteLine(item); }