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

Get all values of a NameValueCollection to a string

var col = new NameValueCollection() { { “a”, “b” }, { “1”, “2” } }; // collection initializer var values = col.Cast<string>().Select(e => col[e]); // b, 2 var str = String.Join(“,”, values ); // “b,2” Also you can create an extension method: public static string Join(this NameValueCollection collection, Func<string,string> selector, string separator) { return String.Join(separator, … Read more

how to convert NameValueCollection to JSON string?

One way to serialize NameValueCollection is by first converting it to Dictionary and then serialize the Dictionary. To convert to dictionary: thenvc.AllKeys.ToDictionary(k => k, k => thenvc[k]); If you need to do the conversion frequently, you can also create an extension method to NameValueCollection: public static class NVCExtender { public static IDictionary<string, string> ToDictionary( this … Read more

How to know if an HTTP request header value exists

if (Request.Headers[“XYZComponent”].Count() > 0) … will attempted to count the number of characters in the returned string, but if the header doesn’t exist it will return NULL, hence why it’s throwing an exception. Your second example effectively does the same thing, it will search through the collection of Headers and return NULL if it doesn’t … Read more

Make NameValueCollection accessible to LINQ Query

You need to “lift” the non-generic IEnumerable to an IEnumerable<string>. It has been suggested that you use OfType but that is a filtering method. What you’re doing is the equivalent of a cast, for which there is the Cast operator: var fields = RequestFields().Cast<string>(); As Frans pointed out, this only provides access to the keys. … Read more

C# Iterate through NameValueCollection

You can flatten the collection with Linq, but it’s still a foreach loop 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 to a (non-nested) collection of anonymous objects with … Read more

NameValueCollection to URL Query?

Simply calling ToString() on the NameValueCollection will return the name value pairs in a name1=value1&name2=value2 querystring ready format. Note that NameValueCollection types don’t actually support this and it’s misleading to suggest this, but the behavior works here due to the internal type that’s actually returned, as explained below. Thanks to @mjwills for pointing out that … Read more

NameValueCollection vs Dictionary [duplicate]

They aren’t semantically identical. The NameValueCollection can have duplicate keys while the Dictionary cannot. Personally if you don’t have duplicate keys, then I would stick with the Dictionary. It’s more modern, uses IEnumerable<> which makes it easy to mingle with Linq queries. You can even create a Dictionary using the Linq ToDictionary() method.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)