Skip to content

Instantly share code, notes, and snippets.

@ismaelhamed
Last active December 25, 2015 02:49
Show Gist options
  • Save ismaelhamed/6905335 to your computer and use it in GitHub Desktop.
Save ismaelhamed/6905335 to your computer and use it in GitHub Desktop.
Some extension methods for the object NameValueCollection
namespace System.Collections.Specialized
{
using System.Collections.Generic;
using System.Linq;
using System.Net;
public static class NameValueCollectionExtensions
{
public static IDictionary<string, string> ToDictionary(this NameValueCollection source)
{
return source.Cast<string>().Select(s => new { Key = s, Value = source[s] }).ToDictionary(p => p.Key, p => p.Value);
}
public static string ToQueryString(this NameValueCollection source)
{
return string.Concat("?", string.Join("&", ArrayEx.ConvertAll(source.AllKeys.ToArray(), key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(source[key])))));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment