简体   繁体   English

检查列表是否<stringcollection>包含列表<string></string></stringcollection>

[英]Check if a list<StringCollection> contains list<string>

Given a list<StringCollection> how do most efficiently check whether all string are contained in any of the StringCollection s?给定一个list<StringCollection>如何最有效地检查所有string是否包含在任何StringCollection中?

Example:例子:

using System;
using System.Collections.Specialized;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        // Create and initializes a new StringCollection.
        StringCollection myCol0 = new StringCollection();
        StringCollection myCol1 = new StringCollection();
        StringCollection myCol2 = new StringCollection();

        StringCollection SearchCol = new StringCollection();

        // Add a range of elements from an array to the end of the StringCollection.
        String[] myArr0 = new String[] { "RED", "car", "boat" };
        myCol0.AddRange( myArr0 );
        
        // Add a range of elements from an array to the end of the StringCollection.
        String[] myArr1 = new String[] { "Blue", "Goku", "Nappa" };
        myCol1.AddRange( myArr1 );
        
        // Add a range of elements from an array to the end of the StringCollection.
        String[] myArr2 = new String[] { "Yellow", "Winter", "Summer" };
        myCol2.AddRange( myArr2 );
        
        // Add a range of elements from an array to the end of the StringCollection.
        String[] myArr3 = new String[] { "Yellow", "Blue", "RED" };
        SearchCol.AddRange( myArr3 );
        
        List<StringCollection> a = new List<StringCollection>();
        a.Add(myCol0);
        a.Add(myCol1);
        a.Add(myCol2);
    }
}

In this case I want to know whether the strings in SearchCol is contained within the stringcollections stored in List<StringCollection> a在这种情况下,我想知道SearchCol中的字符串是否包含在List<StringCollection> a中存储的字符串集合中

In this case I just like to know which of the searchCol strings is not included in the List<StringCollection> a在这种情况下,我只想知道哪些 searchCol 字符串不包含在List<StringCollection> a

The only way I see this it possible to do so is via a double for loop?我看到这一点的唯一方法是通过双 for 循环? Is there any datastructure that would be more efficient rather than an stringcollection?有没有比字符串集合更有效的数据结构?

Is there any datastructure that would be more efficient rather than an stringcollection有没有比字符串集合更有效的数据结构

Efficient in what way?以什么方式有效? Of course you should normally use a IEnumerable<string> (like a string[] vor List<string> ) since StringCollection is not generic.当然,您通常应该使用IEnumerable<string> (如string[] vor List<string> ),因为StringCollection不是通用的。

But you can also use StringCollection , you have to cast each item from object to string:但是您也可以使用StringCollection ,您必须将每个项目从 object 转换为字符串:

var allStrings = a.SelectMany(c => c.Cast<string>());
var searchStrings = SearchCol.Cast<string>();
bool allSearchStringsAreContained = searchStrings.All(allStrings.Contains);

as for the "how do most efficiently", this simple approach is efficient, but if you have a large list of strings that you search or huge string lists, you could use a set based approach:至于“如何最有效地做”,这种简单的方法是有效的,但是如果您有大量要搜索的字符串或大量字符串列表,则可以使用基于集合的方法:

HashSet<string> set = new HashSet<string>(searchStrings);
bool allSearchStringsAreContained = set.IsSubsetOf(allStrings);

Finally, if you want to ignore the case, so treat "RED" and "Red" same:最后,如果您想忽略大小写,那么将“RED”和“Red”视为相同:

Approach 1:方法一:

bool allSearchStringsAreContained = searchStrings.All(s => allStrings.Contains(s, StringComparer.OrdinalIgnoreCase));

Approach 2:方法二:

HashSet<string> set = new HashSet<string>(searchStrings, StringComparer.OrdinalIgnoreCase);

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

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