c# - How to add duplicate keys into the Dictionary -


i have lines text files want add dictionary.i using dictionary first time.while adding starting lines ok got error:

an item same key has been added

here in code there duplicate keys can not change.here code in c#

dictionary<string, string> previouslines = new dictionary<string, string> { }; previouslines.add(dialedno, line); 

here dialedno key , line textfile line. here code retrieving given line based on key.

string tansferorginext = previouslines[dialedno]; 

so concern how allow add duplicate keys in dictionary if possible , if not how can similar functionality.

please me .

how allow add duplicate keys in dictionary

it not possible. keys should unique. dictionary<tkey, tvalue> implemented:

every key in adictionary<tkey, tvalue> must unique according dictionary's equality comparer.

possible solutions - can keep collection of strings value (i.e. use dictionary<string, list<string>>), or (better) can use lookup<tkey, tvalue> instead of dictionary.


how check duplicate keys , delete previous value dictionary?

check if key exists before adding new entry:

if (!previouslines.containskey(dialedno))         previouslines.add(dialedno, line); // add new entry else      previouslines[dialedno] = line; // update entry value 

also can use linq query to create dictionary last value each key (assume have sequence of objects dialedno , line properties):

var previouslines = sequence.groupby(s => s.dialedno)                             .todictionary(g => g.key, g => g.last().line); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -