Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在多关键字字典上进行部分搜索_C#_Linq_Dictionary - Fatal编程技术网

C# 如何在多关键字字典上进行部分搜索

C# 如何在多关键字字典上进行部分搜索,c#,linq,dictionary,C#,Linq,Dictionary,我创建了一个多键字典(RegintsFields是可枚举类型) 我使用ContainsKey搜索字典作为 if (registrantsRepository.ContainsKey(Tuple.Create(registrantId, fieldId, dataId))) 到目前为止,它运行良好 但我只想用2个键搜索字典,即字典中包含的特定registentid和fieldId,但包含任何dataId。换句话说,我喜欢找到像这样的所有项目 var entries = registrantsRe

我创建了一个多键字典(RegintsFields是可枚举类型)

我使用ContainsKey搜索字典作为

if (registrantsRepository.ContainsKey(Tuple.Create(registrantId, fieldId, dataId)))
到目前为止,它运行良好

但我只想用2个键搜索字典,即字典中包含的特定registentid和fieldId,但包含任何dataId。换句话说,我喜欢找到像这样的所有项目

var entries = registrantsRepository(Tuple.Create(registrantId, fieldId, *))

应该如何做(也许在Linq中)?谢谢。

没有这样的通配符搜索,但有一种方法可以在搜索时忽略那些您不感兴趣的字段

遍历的集合,引用您感兴趣匹配的
元组的属性。您可以使用LINQ的
任何
方法来实现这一点

if (registrantsRepository.Keys.Any(x => x.Item1 == registrantId && x.Item2 == fieldId)
{

}

没有像那样的通配符搜索,但有一种方法可以在搜索时忽略那些您不感兴趣的字段

遍历的集合,引用您感兴趣匹配的
元组的属性。您可以使用LINQ的
任何
方法来实现这一点

if (registrantsRepository.Keys.Any(x => x.Item1 == registrantId && x.Item2 == fieldId)
{

}

我只需要创建一个单独的查找

var registrantsByIdAndField = registrantsFields
    .ToLookup(r => Tuple.Create(c.RegistrantID, c.FieldID));
然后,您仍然可以通过以下方式快速查找:

var entries = registrantsByIdAndField[Tuple.Create(registrantId, fieldId)];

我只需要创建一个单独的查找

var registrantsByIdAndField = registrantsFields
    .ToLookup(r => Tuple.Create(c.RegistrantID, c.FieldID));
然后,您仍然可以通过以下方式快速查找:

var entries = registrantsByIdAndField[Tuple.Create(registrantId, fieldId)];

这里都是好答案。
ToLookup
是您的选择吗


编辑:刚刚意识到@StriplingWarrior打败了我

这里都是好答案。
ToLookup
是您的选择吗


编辑:刚刚意识到@StriplingWarrior打败了我

你有什么性能要求吗?你在乎是O(n)还是O(1)才能找到值吗?是的,我需要尽快找到。你决定搜索后是否需要向字典中添加更多项?或者你能考虑字典“关闭”吗?字典可以被认为是关闭的。你有什么性能要求吗?你在乎是O(n)还是O(1)才能找到值吗?是的,我需要尽快找到。你决定搜索后是否需要向字典中添加更多项?或者你能考虑字典“关闭”吗?字典可以被认为是关闭的。在这种情况下,如何获得数据为注册和字段?