首页 > 解决方案 > 无法使用 [] 将索引应用于“System.Collections.Generic.HashSet”类型的表达式'

问题描述

我有两个哈希集。一个由字符串列表组成,另一个是字符串数组列表。

HashSet<string> hashSetpdf = new HashSet<string>();
var hashSetReports = new HashSet<string[]>(DTtoList(dReport));

所以我试图从 hashSetpdf 到 hashSetReports 的所有元素的第一个索引中搜索一个字符串。

实际上,我的以下代码都运行良好,但我的问题是,如果我有很多数据,它会花费很多时间。

        foreach (string c in hashSetpdf)
        {
            if (hashSetReports.Any(r => r.Contains(c)))
            {
                //do something...
            }
            else
            {
                //do something...
            }
        }

我尝试了以下方法,但它给了我一个错误。

        foreach (string c in hashSetpdf)
        {
            if (hashSetReports.Contains(c))
            {
                //do something...
            }
            else
            {
                //do something...
            }
        }

这是我遇到的错误。

Error   5   Cannot apply indexing with [] to an expression of type 
'System.Collections.Generic.HashSet<string[]>'

有没有什么快速的方法来搜索哈希集中字符串数组的第一个索引?

标签: c#hashset

解决方案


因此,您有一组字符串(称为 A),并且您想检查在一组字符串列表(称为集合 B)中,该集合中任何列表中的任何字符串是否具有字符串在您的一组字符串 (A) 中。

这必然需要|A| * \sum_i |B| * n_i 次,其中 n_i 是集合 B 中第 i 个列表的长度,|X| 表示 X 的基数。


推荐阅读