Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Duplicates keys to NameValueCollection

I have read that NameValueCollection allows for duplicate keys to be added, however this does not seem to be the case when I try to use it.

My Code

using (var wb = new WebClient())
{
     var data = new NameValueCollection();
     var sourceData = (List<Dictionary<string, object>>)dic["mapData"];
     var countSource = sourceData.Count;

     foreach (var item in (List<Dictionary<string, object>>)dic["mapData"])
     {
          data.Add("pp", item["Latitude"].ToString() + "," + item["Longitude"].ToString());
     }

     var dataCount = data.Count;
     var response = wb.UploadValues(@"http://dev.virtualearth.net/REST/V1/Imagery/Map/road/?mapArea=" + swLat.ToString() + "," + swLong + "," + neLat + "," + neLong + "&mapSize=800,600&key=" + key, "POST", data);

     return this.LargeJson(new { imageData = CreateBase64Image(response) });
}

What I am observing

my sourceData contains 36 items

watches

I am iterating through sourceData and adding items to my NameValueCollection data and add items with the same key "pp"

I was expecting to have 36 items in my data NameValueCollection, but I am only getting 1 and all of my values get appended to the same key.

What am I missing ?

like image 565
G-Man Avatar asked Feb 20 '26 05:02

G-Man


1 Answers

This is the expected behaviour - see the documentation:

If the specified key already exists in the target NameValueCollection instance, the specified value is added to the existing comma-separated list of values in the form "value1,value2,value3". The values are associated with the same key in the target NameValueCollection instance.

like image 87
Lee Avatar answered Feb 22 '26 19:02

Lee