Skip to content

Instantly share code, notes, and snippets.

@phaniav
Created September 12, 2017 22:24
Show Gist options
  • Save phaniav/564e157a48fd44891f7e6962f8ecd5fc to your computer and use it in GitHub Desktop.
Save phaniav/564e157a48fd44891f7e6962f8ecd5fc to your computer and use it in GitHub Desktop.
Dictionary to Querystring convertor
using System.Collections.Generic;
using System.Linq;
using System.Text;
public static class CollectionExtensions
{
public static string ToQueryString(this IDictionary<string, string> dict)
{
if (dict.Count == 0) return string.Empty;
var buffer = new StringBuilder();
int count = 0;
bool end = false;
foreach (var key in dict.Keys)
{
if (count == dict.Count - 1) end = true;
if (end)
buffer.AppendFormat("{0}={1}", key, dict[key]);
else
buffer.AppendFormat("{0}={1}&", key, dict[key]);
count++;
}
return buffer.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment