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
Post a Comment