C # Iteration through NameValueCollection

I have a NameValueCollection and you want to NameValueCollection over the values. I'm currently doing this, but there seems to be a tidier way to do this:

 NameValueCollection nvc = new NameValueCollection(); nvc.Add("Test", "Val1"); nvc.Add("Test2", "Val1"); nvc.Add("Test2", "Val1"); nvc.Add("Test2", "Val2"); nvc.Add("Test3", "Val1"); nvc.Add("Test4", "Val4"); foreach (string s in nvc) foreach (string v in nvc.GetValues(s)) Console.WriteLine("{0} {1}", s, v); Console.ReadLine(); 

Whether there is a?

+70
collections c # namevaluecollection
Feb 21 '11 at 12:30
source share
7 answers

You can flatten the collection using Linq, but it's still a foreach , but now more implicit.

 var items = nvc.AllKeys.SelectMany(nvc.GetValues, (k, v) => new {key = k, value = v}); foreach (var item in items) Console.WriteLine("{0} {1}", item.key, item.value); 

The first line converts the nested collection into a (non-nested) collection of anonymous objects with a property key and value.

It is smoothed in such a way that now it is a mapping key โ†’ instead of a key โ†’ a set of values. Example data:

Before:

Test โ†’ [Val],

Test2 โ†’ [Val1, Val1, Val2],

Test3 โ†’ [Val1],

Test4 โ†’ [Val4]

After:

Test โ†’ Val,

Test2 โ†’ Val1,

Test2 โ†’ Val1,

Test2 โ†’ Val2,

Test3 โ†’ Val1,

Test4 โ†’ Val4

+91
Feb 21 '11 at 13:29
source share

You can use the key to search instead of two loops:

 foreach (string key in nvc) { Console.WriteLine("{0} {1}", key, nvc[key]); } 
+76
Feb 21 '11 at 12:34
source share

Nothing new to see here (@Julian + 1'd me answer is functionally equivalent), y'all move along y'all, please.




I have an [overkill for this case, but possibly relevant] set of extension methods in response to the corresponding question , which will allow you to do:

 foreach ( KeyValuePair<string,string> item in nvc.AsEnumerable().AsKeyValuePairs() ) Console.WriteLine("{0} {1}", item.key, item.value); 
+3
Oct 03 '12 at 13:13
source share

The only way to avoid nested loops is to use an extra list to store the values:

 List<string> arrValues = new List<string>(); for (int i = 0; i < nvc.Count; i++) arrValues.AddRange(nvc.GetValues(i)); foreach (string value in arrValues) Console.WriteLine(value); 

(Requires [only] .NET 2.0 or later)

+1
Feb 21 '11 at 12:44
source share

I think this is easier:

 For i As Integer = 0 To nvc.Count - 1 Console.Write("No", "Key", "Value") Console.Write(i, nvc.GetKey(i), nvc.Get(i)) Next 
+1
Dec 17 '17 at 11:27
source share
 var enu = myNameValueCollection.GetEnumerator(); while (enu.MoveNext()) { string key = (string)enu.Current; string value = myNameValueCollection[key]; } 

OR when the keys are reset to zero:

 for (int i = 0; i < myNameValueCollection.Count; i++) { string key = myNameValueCollection.GetKey(i); string value = myNameValueCollection.Get(i); } 
0
Jun 03 '19 at 16:15
source share
 foreach ( string key in nvc.Keys ) Console.WriteLine("{0} {1}", key, nvc[key]); 

This will return all keys and corresponding values โ€‹โ€‹to you.

-2
Feb 21 '11 at 12:34
source share



All Articles