C#: Convert Dictionary to NameValueCollection
Why not use a simple foreach loop? foreach(var kvp in dict) { nameValueCollection.Add(kvp.Key.ToString(), kvp.Value.ToString()); } This could be embedded into an extension method: public static NameValueCollection ToNameValueCollection<TKey, TValue>( this IDictionary<TKey, TValue> dict) { var nameValueCollection = new NameValueCollection(); foreach(var kvp in dict) { string value = null; if(kvp.Value != null) value = kvp.Value.ToString(); nameValueCollection.Add(kvp.Key.ToString(), value); … Read more