简体   繁体   English

检查列表是否包含其他列表。 C#

[英]Check if list contains another list. C#

EDIT, just saying the comment in the ContainsAllItem explains best. 编辑,只是说ContainsAllItem中的注释解释得最好。

Im sorry for asking, I know this was asked before, but I just did not get it. 我很抱歉要问,我知道之前有人问过这个问题,但我只是没有得到它。 Ok, so I want to check If a list contains all the items in another list WITHOUT overlapping, aswell as compare the items based on the classes string, name variable(called itemname and it is public). 好的,所以我想检查一个列表是否包含另一个列表中没有重叠的所有项目,以及根据类字符串比较项目,名称变量(称为itemname,它是公共的)。

public class Item
{
    public string itemname;
}

So basically, have a class(lets say.. class A) with a list of items, and a function that checks takes the list of items of class A, and then compares it to another list(lets call it B), but compare it by the itemname variable not the whole item. 所以基本上,有一个类(比如说..类A)带有一个项目列表,一个函数检查获取类A的项目列表,然后将它与另一个列表进行比较(让我们称之为B),但比较它由itemname变量而不是整个项目。

And most importantly could you explain in detail what it does. 最重要的是,你能否详细解释它的作用。

So how the function/class would look as of now. 那么函数/类现在看起来如何。

public class SomeClass
{
    public List<Item> myItems = new List<Item>();

    public bool ContainsAllItems(List<Item> B)
    {
        //Make a function that compares the to lists by itemname and only returns true if the myItems list contains ALL, items in list b.
        //Also could you explain how it works.
    }
}

I haven't checked the pref on this, but linq does have the Except operator. 我还没有检查过pref,但是linq确实有Except运算符。

 var x = new int[] {4,5};
 var y = new int[] {1 ,2 ,3 ,4 ,5};   

 y.Except(x).Any(); //true, not all items from y are in x
 x.Except(y).Any(); // false, all items from x are in y

This isn't exactly what you asked for, but performance wise you should definitely use HashSet 's IsProperSubsetOf . 这不完全是你要求的,但是性能方面你绝对应该使用HashSetIsProperSubsetOf It can do what you want in orders of magnitude less time: 它可以在数量级更短的时间内完成您想要的任务:

HashSet<string> a = new HashSet<string>(list1.Select(x => x.itemname));
HashSet<string> b = new HashSet<string>(list2.Select(x => x.itemname));

a.IsProperSubsetOf(b)

Explanation: HashSet uses the item's GetHashCode value and Equals method in an efficient way to compare items. 说明: HashSet以有效的方式使用项的GetHashCode值和Equals方法来比较项。 That means that when it internally goes through the values in b it doesn't have to compare it to all other items in a. 这意味着当它在内部遍历b的值时,它不必将其与a中的所有其他项进行比较。 It uses the hash code (and an internal hash function) to check whether it already has that value or doesn't. 它使用哈希代码(和内部哈希函数)来检查它是否已经具有该值或不具有该值。

Because it does only a single check for every item (each check is O(1)) it's much faster than checking all items in a which would take O(n) (for each item in b that is). 因为它只对每个项目进行一次检查(每个检查是O(1)),所以它比检查O(n)中a 所有项目要快得多(对于b中的每个项目)。

B.All(itB=>myItems.Select(itA=>itA.ItemName).Contains(itB.ItemName))

将在O(N ^ 2)时间运行,但很酷,你可以在一条相当难以理解的线上做到这一点。

Here is another way. 这是另一种方式。 I included a way to include and exclude the list comparison. 我提供了一种包含和排除列表比较的方法。

var a = new List<int> { 1, 2, 3, 4, 5 };

var b = new List<int> { 1, 2, 3 };

//Exists in list a but not in b
var c = (from i 
            in a 
         let found = b.Any(j => j == i) 
         where !found select i)
         .ToList();

//Exists in both lists
var d = (from i 
            in a  
         let found = b.Any(j => j == i) 
         where found select i)
         .ToList();

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

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