C# 如何在字典中找到“第一个”值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1731704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find a 'first' value in a Dictionary?
提问by IAbstract
How can I find the first value in a Dictionary<int, MyTableClass>
where MyTableClass inherits Field<F1, F2, F3>
? I'd prefer a Property or Property/Method combination that returns the first value in the Dictionary where F1 = MyEnum.value
.
如何Dictionary<int, MyTableClass>
在 MyTableClass 继承的地方找到第一个值Field<F1, F2, F3>
?我更喜欢返回字典中第一个值的属性或属性/方法组合 where F1 = MyEnum.value
。
What I don't want to do is a foreach
. Performance-wise, this is really not a preferred method.
我不想做的是一个foreach
. 在性能方面,这确实不是首选方法。
采纳答案by JaredPar
No matter how you dress it up here you are going to essentially have to do a foreach
over the values in the Dictionary
. A Dictionary<TKey,TValue>
provides close to O(1) access for a full key to a given value. It is not designed to provide efficient access to a partial key. In order to get that you would need to keep a second Dictionary
instance making the appropriate mapping.
不管你如何装扮它在这里你将基本上是不得不做foreach
了的值Dictionary
。ADictionary<TKey,TValue>
为给定值的完整键提供接近 O(1) 的访问。它并非旨在提供对部分密钥的有效访问。为了得到它,您需要保留第二个Dictionary
实例来进行适当的映射。
回答by maxpower47
You could use the .First() extension method.
您可以使用 .First() 扩展方法。
回答by Pavel Minaev
The shortest way to find a value matching some criteria (I couldn't quite understand what you want specifically - first F1
is a generic type parameter, and then you use ==
to compare it as if it was a value...) is to do this:
找到匹配某些条件的值的最短方法(我不太明白你想要什么 - 首先F1
是一个泛型类型参数,然后你==
用来比较它,好像它是一个值......)是这样做:
dictionary.Values.First(x => ...);
where ...
will be a boolean expression on x
. However, this will not be any faster than foreach
...because you're not doing a lookup on dictionary key. Only key lookup is fast; for anything else, you have to do a linear scan (or maintain a second dictionary for a different key, which will be whatever you do lookups on).[copied from comment]
where...
将是一个布尔表达式x
。但是,这不会比foreach
...更快,因为您没有对字典键进行查找。只有键查找速度快;对于其他任何事情,您必须进行线性扫描(或为不同的键维护第二个字典,这将是您查找的任何内容)。[复制自评论]
回答by Guffa
The dictionary doesn't maintain any specific order between element, so there isn't really any element that can be the first one unless you specify some ordering.
字典不维护元素之间的任何特定顺序,因此除非您指定某种顺序,否则实际上没有任何元素可以成为第一个元素。
You can get the first item that the dictionary happens to find like this:
您可以获得字典碰巧找到的第一项,如下所示:
MyTableClass one = dict.Where(pair => pair.Value.F1 == MyEnum.value).First();
This will just loop through the items until it finds a match, so you are just using the dictionary as a list. If you want any performance, you should have a dictionary where the value from F1 is the key.
这将循环遍历项目直到找到匹配项,因此您只是将字典用作列表。如果你想要任何性能,你应该有一个字典,其中 F1 的值是键。