c# - How to identify unique dictionary key of type <RateIdentifier> with certains members equal to something? -


i have dictionary _customrates keys of type rateidentifier , values of type customrate. type t looks this:

public rateidentifier(int id,           string name,          currency currency1,          currency currency2) 

now, have particular currency1 , currency2, , want check whether there exists rateidentifier in dictionary these specified currencies, , if return key (if not return default). know if such key exists only key has particular currency1 , currency2 (i.e. 2 currencies ensure uniqueness of particular key).

i'm finding difficult because although know how use .where on dictionary, doesn't seem appropriate solution since know have @ 1 such key, seems overkill check entire dictionary every time of these dictionaries particularly huge.

use firstordefault, if overriding gethashcode() , equals() not possibility:

var mystuff = _customrates.firstordefault(x => x.key.currency1 == mycurrency1 && x.key.currency2 == mycurrency2); 

however, can (and possibly should) override gethashcode() , equals() on key type (if haven't already) , make them return same hash code same currencies. way, can use standard facilities of dictionary:

// assume gethashcode , equals overridden var mystuff = _customrates[new rateidentifier() { currency1 = mycurrency1, currency2 = mycurrency2 }]; 

when looking values, dictionary first calculate hash code (using gethashcode) of specified key , search that. if multiple elements in dictionary have same hash code, equals called determine 1 matches key. means that, if not have specific key object used when added value dictionary, can still construct new key.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -