【问题标题】:Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.HashSet<string[]>'无法将带有 [] 的索引应用于“System.Collections.Generic.HashSet<string[]>”类型的表达式
【发布时间】:2019-12-08 14:16:28
【问题描述】:

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

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[]>'

有没有什么快速的方法可以在hashset中搜索字符串数组的第一个索引?

【问题讨论】:

  • 您是否考虑过字符串哈希集数组,而不是字符串数组的哈希集?还是字符串散列集的散列集?
  • 如果没有自定义比较器,我不确定HashSet&lt;string[]&gt; 是否符合您的预期。请参阅 this examplethis example。除非你传递完全相同的数组实例Contains will return false.

标签: c# hashset


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2011-04-06
    • 2019-03-20
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多