Created
September 12, 2017 22:24
-
-
Save phaniav/564e157a48fd44891f7e6962f8ecd5fc to your computer and use it in GitHub Desktop.
Dictionary to Querystring convertor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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