Leon Barkan Leon Barkan - 1 month ago 15
C# Question

How to get key object from Dictionary by key

In my application, I have a dictionary with key/value objects

Dictionary<TObject,VObject> list


To check if I have the object, I'm using a special instance of
TObject
and checking if it exists in the list.

After i get
TRUE
, I need to get the original Key object (
TObject
)

VObject result = list[TObject]; // will return me the VObject


The original
TObject
(the key), has more values that have been initialized on the object creation and I need to do a test on them.

How can i do this?

Answer

The dictionary object contains Keys property, please see https://msdn.microsoft.com/en-us/library/yt2fy5zk(v=vs.110).aspx for more information.

You can run queries against this property to select the key you are looking for.

Useage with linq;

var foundKey = dictionaryObject.Keys.FirstOrDefault( * Linq Expression* );