Home > Enterprise >  C# How can I print all elements from List<Dictionary<string,string>> on the console?
C# How can I print all elements from List<Dictionary<string,string>> on the console?

Time:04-16

I’m a rookie in programming and I have a problem understanding how to print elements from a List. In the task I’ve been given, I receive:

List<Dictionary<string,string>>() list = new 
List<Dictionary<string,string>>(); 
list.Add(processString(string, string));
list.Add(processString(string, string));

The processStrig is a Dictionary<string,string> and the keys are the same for both records.

I tried to create a new Dictionary and then populate it with foreach:

    Dictionary<string,string>() dict = new Dictionary<string, string>();
    foreach (Dictionary<string,string>r in list)
    {
        foreach (string inner in r.Keys)
        {
            if (!dict.ContainsKey(inner))
            {
                dict.Add(inner, r[inner]);
            }
        }
    } 

    

and then print the new dict with another foreach, but it shows me only the first input because the keys are the same. So basically my question is how to print the both inputs? The output should look like this:

The output should look like this:

[0]
"count":"some string"
"order":"some string"
[1]
"count":"some other string"
"order":"some other string"

CodePudding user response:

If you are looking for a loop solution, you can try something like this:

 List<Dictionary<string, string>> list = ...

 for (int i = 0; i < list.Count;   i) { 
   Console.WriteLine($"[{i}]");

   if (list[i] == null)
     Console.WriteLine("[null]");
   else 
     foreach (var pair in list[i])
       Console.WriteLine($"\"{pair.Key}\" : \"{pair.Value}\"");  
}
 

CodePudding user response:

Let's have a method that makes you a dictionary:

public static Dictionary<string, string> MakeMeADictionary(string value1, string value2){
  var d = new Dictionary<string, string>();
  d["key1"] = value1;
  d["key2"] = value2;
  return d;
}

Let's call it twice, adding the results to a List:

var listOfDicts = new List<Dictionary<string, string>>();

listOfDicts.Add(MakeMeADictionary("first val", "second val"));
listOfDicts.Add(MakeMeADictionary("third val", "fourth val"));

Let's enumerate the list, and then each dictionary inside it:

foreach(var dict in listOfDicts){

  Console.WriteLine("Enumerating a dictionary");

  foreach(var keyValuePair in dict)
    Console.WriteLine($"Key is: {keyValuePair.Key}, Value is: {keyValuePair.Value}");

}

Result:

Enumerating a dictionary
Key is: key1, Value is: first val
Key is: key2, Value is: second val
Enumerating a dictionary
Key is: key1, Value is: third val
Key is: key2, Value is: fourth val

Strive for variable names that make your code make sense; plurals or names of colelction types for collections, foreach vars that singularly make sense for the plural being enumerated etc.. If this were a less contrived example, and e.g. it were a List<Person> I'd call it people, perhaps, and have foreach(var person in people).. I couldn't understand your choice of r in foreach(var r in list)

  • Related