【发布时间】: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<string[]>是否符合您的预期。请参阅 this example 和 this example。除非你传递完全相同的数组实例,Containswill returnfalse.