简体   繁体   English

从主列表中删除找到的项目<T>

[英]Removing Found Items from a Master List<T>

Okay, So I have two lists, MasterTagList and FoundTagList. 好的,所以我有两个列表,MasterTagList和FoundTagList。 The master list is set before hand with what tags should be found. 预先设置了主列表以及应该找到哪些标签。 The found list is made up of the tags that the reader actually finds. 找到的列表由读者实际找到的标签组成。 What I'm trying to do is determine the items in the MasterTagList that were not anywhere in the found tag list, meaning they are completely absent. 我想做的是确定MasterTagList中没有在找到的标签列表中任何位置的项目,这意味着它们完全不存在。

I have tried to use the Except() method, but for some reason, if the items are not in the exact order, then it says that all of the items are still missing. 我试图使用Except()方法,但是由于某些原因,如果项目的顺序不正确,则说明所有项目仍然缺失。

I also tried code that looked something like this: 我还尝试了看起来像这样的代码:

List<string> missing = new List<string>();
foreach (string T in MasterTagList)
{
    if (!FoundTagList.Contains(T))
    {
        missing.Add(T);
    }
}

I would think that the new list, missing, should show my all of the items that are in MasterTagList, but not in the FoundTagList, regardless of order. 我认为缺少的新列表应该显示我所有在MasterTagList中的项目,而不是在FoundTagList中的所有项目,而不管顺序如何。 I am not an expert in C# by any manner, so I might be miss using something. 我无论如何都不是C#的专家,所以我可能会错过使用某些东西的机会。

I just want to know if there is a way I can modify this code to get it to work, or if there is any other way of going about finding the missing elements, regardless of order. 我只想知道是否有一种方法可以修改此代码以使其正常工作,或者是否有其他方法可以找到丢失的元素,而无需考虑顺序。

Thanks in advance! 提前致谢!

EDIT: Like I said, I have tried every other option I have seen suggested on here. 编辑:就像我说的,我已经尝试过在这里看到建议的所有其他选择。 My Except() instance looked something like this: 我的Except()实例看起来像这样:

List<string> missing = MasterTagList.Except(FoundTagList).ToList;

Yet still, when I run the program, the missing list is always identical to the MasterTagList, although FoundTagList and MasterTagList both contain identical elements, just in different orders. 但是,当我运行程序时,尽管FoundTagList和MasterTagList都包含相同的元素,只是顺序不同,但丢失的列表始终与MasterTagList相同。

SOLVED: It turns out that something must have been wrong with the way I was adding things to the lists originally; 已解决:原来我最初将内容添加到列表中的方式一定有问题; therefore, the elements in each list were not necessarily identical. 因此,每个列表中的元素不一定相同。 I went back and modified how I was adding things to the list and now my program is working perfectly. 我回过头来修改了如何将内容添加到列表中,现在我的程序可以正常运行了。 Thank-you to all of you who had suggestions! 谢谢所有提出建议的人!

It can be pretty straightforward with the Except method 使用Except方法可以非常简单

List<string> missing = MasterTagList.Except(FoundTagList).ToList();

EDIT 编辑

This can be verified with the following. 可以用以下方法验证。 Note that the order of the lists do not matter. 请注意,列表的顺序无关紧要。 It will still produce an empty list (none missing). 仍然会产生一个空列表(不丢失)。

List<string> Master = new List<string>()
    { "a", "b", "c", "d", "e", "f", "g", "h" };

List<string> Found = new List<string>() 
    { "g", "d", "e", "h", "a", "b", "c", "f" };

List<string> missing = Master.Except(Found).ToList();

Console.WriteLine(missing.Count); //Prints 0
List<string> missing = MasterTagList.Where(s => !FoundTagList.Contains(s)).ToList();

Use a Set instead of a List. 使用集合而不是列表。 Sets only permit a single instance of a value to exist in the collection at once (according to the "Equals" method or a separate equality comparer). 集仅允许值的单个实例一次存在于集合中(根据“等于”方法或单独的相等比较器)。

Also, the "Except" method will work correctly as sets don't have any ordering. 同样,“ Except”方法将正确运行,因为集没有任何排序。

http://msdn.microsoft.com/en-us/library/bb359438.aspx http://msdn.microsoft.com/en-us/library/bb359438.aspx

Using a set will also provide an additional benefit: you don't need to check if a string already exists in the found tags list before trying to add it. 使用集合还可以带来额外的好处:在尝试添加字符串之前,无需检查字符串是否已存在于找到的标签列表中。 If you try to add an already existing item to a set, nothing happens. 如果您尝试将现有项目添加到集合中,则不会发生任何事情。 The HashSet is also very efficient for this as it uses a hashtable to quickly find existing items. HashSet对此非常有效,因为它使用哈希表快速查找现有项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM