C# .NET - Iterate a Dictionary<string,List<string>>

Asked By Sooriya R on 17-Feb-11 03:00 AM
Hi

I want to iterate a dictionary of string, List<string>. for this i am using 3 foreach loop and i got the result. Suggest me some standard way with less code(foreach).

Thanks,
Sooriya

Venkat K replied to Sooriya R on 17-Feb-11 03:12 AM
Hi Sooriya,
Here is how you can Iterating through some list of orders:

List<OrderQuery.Order> orders = qy.Execute();
foreach (OrderQuery.Order order in orders)
{
    orders.Add(order.Object);
}

Thanks
Sooriya R replied to Venkat K on 17-Feb-11 03:21 AM
Hi Rajesh,

I used the following code to iterate the dictionary.

foreach (var item in Collection.Keys)

{

  foreach (var item1 in Collection.Values)

  {

    foreach (var item2 in item1)

    {

      Console.WriteLine( item + item2);

    }

  }

}

i ask how to obtain the same with less no of foreach or anyother way.
Thanks.

Mash B replied to Sooriya R on 17-Feb-11 04:30 AM
You can follow below pattern for shortest code

Dictionary<string, List<string>> texts = new Dictionary<string, List<string>>();
 
       foreach(string item in texts.Keys)
       {
         List<string> lstItems = texts[item];
 
         foreach (string lstValueItem in lstItems)
         {
            
         }
       }