where MyTableClass inherits Fie...">

How to find the "first" meaning in a dictionary?

How can I find the first value in Dictionary<int, MyTableClass>where MyTableClass inherits Field<F1, F2, F3>? I would prefer a combination of properties or properties / methods that returns the first value in the dictionary, where F1 = MyEnum.value.

What I do not want to do is foreach. For performance, this is really not the preferred method.

+5
source share
4 answers

No matter how you dress it here, you essentially need to do foreachover the values ​​in Dictionary. A Dictionary<TKey,TValue>provides access close to O (1) for the full key to the given value. It is not intended to provide efficient access to a partial key. To do this, you will need to save a second instance Dictionarythat creates the corresponding mapping.

+6
source

The shortest way to find a value that matches some criteria (I could not fully understand what you want specifically - first F1is a typical type parameter, and then you use it ==for comparison, as if it had a value ...) is as follows:

dictionary.Values.First(x => ...);

... x. , foreach... . ; - ( , , ). [ ]

+6

- , , , .

, :

MyTableClass one = dict.Where(pair => pair.Value.F1 == MyEnum.value).First();

, , . - , , F1 .

+5

.First().

+2

All Articles