-3
  1. There are different count and different order of dictionaries
  2. If the key of list A and list B match, I would like to bring the value of list B and modify the value of list A.

Sample Code :

public static List<Dictionary<string, string>> ListDicOne = new List<Dictionary<string, string>>();

public static List<Dictionary<string, string>> ListDicTwo = new List<Dictionary<string, string>>();

(...)
ListDicOne[0].Add("ABCD", "");          // empty value
ListDicOne[1].Add("ABCD", "");          
ListDicOne[2].Add("EFGH", "test");

ListDicTwo[0].Add("AABC", "oooo");
ListDicTwo[1].Add("ABCD", "WOW");
ListDicTwo[2].Add("CCDD", "haha");
ListDicTwo[3].Add("CCDD", "haha");

expected result value :

// Console Write key and value of ListDicA 

ABCD \t WOW
ABCD \t WOW
EFGH \t test
Caglia
  • 9
  • 5
  • 1
    Look at this answer: https://stackoverflow.com/a/9547463/3247471 Hope it helps. – Raul Marquez May 30 '20 at 22:55
  • 1
    why you use List of dictionary, i think just dictionary is sufficient. – Mohammed Sajid May 30 '20 at 23:01
  • 2
    I agree with @Sajid, it seems you are using a `Dictionary` like if it was a `KeyValuePair`. – Andrew May 30 '20 at 23:02
  • After your code update, note that you have **seven (7)** different dictionaries, each one with just one element, stored in two lists. I don't think that's what you are trying to do. – Andrew May 30 '20 at 23:10
  • In conclusion, what I want to do is to update the value of A based on B. – Caglia May 30 '20 at 23:18
  • @Caglia yeah we understand what you want, but the question why use List of dictionary and you used it like list of KeyValuePair? you can use just a dictionary or list of KeyValuePair instead list of dictionary. i'm agree with you, if i see `ListDicOne[0].Add("ABCD", "");` and `ListDicOne[0].Add("ABCDEF", "test2");` to show us utility of list of dict. – Mohammed Sajid May 30 '20 at 23:20
  • @Caglia, did you check the post Raul linked? I think that should work. But you should change your code to be `Dictionary ListDicOne = new Dictionary()` and `ListDicOne.Add("ABCD", "")`, `ListDicTwo.Add("AABC", "oooo")`, etc... (Removing those []) – Andrew May 30 '20 at 23:25
  • `If the key of list A and list B match` list are not key value pairs. What did you mean by that ? – Supun De Silva May 30 '20 at 23:25
  • @Caglia i want to say by *i'm agree with you, if i see ListDicOne[0].Add("ABCD", ""); and ListDicOne[0].Add("ABCDEF", "test2"); to show us utility of list of dict* ==> `listOfDicOne[0] = new Dictionary { { "ABCD", "" }, { "ABCDE", "test2" } };` – Mohammed Sajid May 30 '20 at 23:31
  • @Andrew I checked, but the problem is that A and B lists contain the same key. – Caglia May 30 '20 at 23:33
  • I'm sorry for my poor English and poor explanation. – Caglia May 30 '20 at 23:35
  • 1
    First, fix your lists and make them dictionaries. Otherwise, NO solution will work for you. If you don't understand this, we cannot move any further. – Andrew May 30 '20 at 23:35
  • So you have 2 Lists of Dictionaries, And You want to go through each dictionary of List 1 and if any of those dictionaries has a key that matches a key in **any** dictionary of List 2, override that value, Other vice you cannot get the expected output. Correct me if I am wrong. If I am right this is a bit weird requirement – Supun De Silva May 30 '20 at 23:51

1 Answers1

0

I will take a guess and assume you really only want to deal with two dictionaries, and not two lists of many one-item dictionaries, which would not make any sense at all.

First, the dictionaries should be declared like this (I also assume they don't have to be public):

private static Dictionary<string, string> ListDicOne = new Dictionary<string, string>();

private static Dictionary<string, string> ListDicTwo = new Dictionary<string, string>();

And then, the usage could be like this:

ListDicOne.Add("AABC", ""); // I changed the key, they must be unique
ListDicOne.Add("ABCD", "");
ListDicOne.Add("EFGH", "test");

ListDicTwo.Add("AABC", "oooo");
ListDicTwo.Add("ABCD", "WOW");
ListDicTwo.Add("CCDD", "haha");
ListDicTwo.Add("CCDE", "haha"); // I changed the key, they must be unique

foreach (var item in ListDicTwo)
{
    if (ListDicOne.ContainsKey(item.Key))
        ListDicOne[item.Key] = ListDicTwo[item.Key];
}

The final state of ListDicOne is:

("AABC", "oooo")
("ABCD", "WOW")
("EFGH", "test")

I hope you find this clarifying and useful.

** Update **

Considering a string-string list with non-unique keys:

private static List<Tuple<string, string>> ListOne = new List<Tuple<string, string>>();

private static List<Tuple<string, string>> ListTwo = new List<Tuple<string, string>>();

(...)

ListOne.Add(Tuple.Create("ABCD", ""));
ListOne.Add(Tuple.Create("ABCD", ""));
ListOne.Add(Tuple.Create("EFGH", "test"));

ListTwo.Add(Tuple.Create("AABC", "oooo"));
ListTwo.Add(Tuple.Create("ABCD", "WOW"));
ListTwo.Add(Tuple.Create("CCDD", "haha"));
ListTwo.Add(Tuple.Create("CCDD", "haha"));

foreach (var l2item in ListTwo)
{
    for (int i = 0; i < ListOne.Count; i++)
    {
        if (ListOne[i].Item1 == l2item.Item1)
            ListOne[i] = Tuple.Create(l2item.Item1, l2item.Item2);
    }
}

Note 1: tuples are immutable, so you need to create a new one when changing Item2.

Note 2: You cannot use foreach when you need to alter the iterated list, therefore I used a for instead.

Note 3: If ListTwo happens to have a two different key-value combinations (supposedly won't happen), the second one will overwrite the first.

This is the end result with your test values:

("ABCD", "WOW")
("ABCD", "WOW")
("EFGH", "test")
Andrew
  • 6,492
  • 2
  • 29
  • 38
  • I wanted the Dictionary to have a duplicated key. It seems to be a strange request, but the key,value of ListDicOne was read from a text file and did not want to remove the line even if it was duplicated. But I think it's right to unique the key. – Caglia May 31 '20 at 00:10
  • If they key is not unique, then it's not a key and you can't use a dictionary. You could use a `List>`, which is simply a list of tuples with two strings each. But, if a key is repeated in list 1, should the value from list 2 apply to both? And what if list 2 has a repeated key with different values? Or is that not possible? If the repeated key-value pairs are exactly the same, you could do a `Distinct()` on the list and use a `Dictionary` nevertheless. Check out [this question](https://stackoverflow.com/questions/11409587/is-there-dictionarykey-value-without-unique-key). – Andrew May 31 '20 at 00:16
  • @Andrew In that case, we can update it based on the key we find first. Or if List B has the same key, the value should all be the same. As you said, I think my request was illogical. – Caglia May 31 '20 at 00:24
  • @Caglia, I added another approach which might be useful for you if you want to support duplicate *"keys"*. If you also want to add "AABC" to `ListOne`, I guess you can find out yourself how to do that. But feel free to ask if necessary. – Andrew May 31 '20 at 01:36
  • 1
    @Andrew Thank you for your kind answer. It was helpful – Caglia Jun 01 '20 at 13:06