c# - Merge two dictionaries and remove duplicate keys and sort by the value -


i have merge 2 dictionaries 1 dictionary removing duplicate entries , add if not present in first dictionary.

 dictionary<int, string> firstdict = new dictionary<int, string>();  firstdict.add(1, "x");  firstdict.add(2, "b");   dictionary<int, string> seconddict = new dictionary<int, string>();  seconddict.add(1, "m");  seconddict.add(4, "a"); 

result should this:

{4, "a"} {2, "b"} {1, "x"} 

you can use concat sample linq achieve want. here is:

dictionary<int, string> result =     firstdict.concat(seconddict.where(kvp => !firstdict.containskey(kvp.key)))             .orderby(c=>c.value)             .todictionary(c => c.key, c => c.value); 

the result is:

{4, "a"} {2, "b"} {1, "x"} 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -