linq - Merging Dictionary containing a List in C# -


It's related, how to merge two dictionaries in C. An elegant Linq solution is presented, which is cool.

However, this question is dictionary & amp; Object 1 is related to object 2>, , while I have a dictionary where the value is a list & lt; Object2 & gt;

I have the following requirements with the dictionary & amp; Object 1, list & lt; Object2 & gt;

  • If Dictation 1 has the same key as the dictionary 2, then its List & lt; Object2 & gt; The list should end with a new key-value-pair with the share key, and the combined lists from two dictionaries
  • If there is a key in 1 that Dictionary 2 is not then, then list & lt; Object 2 & gt; Must be priced from list 1, and should be opposite.

This is not possible in Linq, or it may be worth writing for a loop for a long time, but it would be good for an elegant solution.

I would suggest creating an own extension It will be more efficient and easy to modify.

  Public static zero merchandics & lt; OBJ1, OBJ2 & gt; (This idictionary & lt; OBJ1, list & lt; oBJ2 & gt; & gt; dict1, IDCcare & lt; OBJ1, list & lt; OBJ2 & gt; & gt; dict2) {foreach (var kvp2 in dict2) { / / If the dictionary already has a key, then merge them (dict1.ContainsKey (kvp2.Key)) {dict1 [kvp2.Key] .advn (kvp2.Value); to continue; } Dict1.Add (kvp2); }}  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -