-2

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" 
5
  • How do you want to format your list? If you are only printing for debugging purposes and don't care much about the format you could simply serialize to JSON and print that, e.g. via var text = System.Text.Json.JsonSerializer.Serialize(list, new JsonSerializerOptions { WriteIndented = true }); ... Oh I see, the required format was shown in your original question without proper formatting. But why are you trying to add the inner dictionaries to some big outer dictionary? Why not just loop through the list, print a counter, then print each inner dictionary, using nested foreach loops? Commented Apr 15, 2022 at 20:17
  • At first learn what is a dictionary and how to use , here for example. tutorialsteacher.com/csharp/csharp-dictionary . Only after this ask questions if you still need Commented Apr 15, 2022 at 20:20
  • Note that Dictionary<TKEY,TVALUE> implements enumeration on KeyValuePair: use foreach (var entry in r) Console.WriteLine($"\"{entry.Key}\": \"{entry.Value}\""); Commented Apr 15, 2022 at 20:22
  • and the keys are the same for both records. - but the values differ? You call processString (note: C# methods should have PascalCase names) twice, and it produces different results each time? Commented Apr 15, 2022 at 20:56
  • thank you guys for the answers. @Serge, thank you for the given resources! I needed it :) Commented Apr 16, 2022 at 8:39

2 Answers 2

0

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}\""); } 
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.