Home > DeveloperSection > Forums > Convert query string to key-value pair
Jeet Verma
Jeet Verma

Total Post:110

Points:776
Posted on    September-03-2014 11:17 PM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 800  View(s)
Rate this:

If I have a string like so:

"Name=Irwin&Home=Caribbean&Preference=Coffee"

is there a method in C# that can convert that to a key-value pair similar to Request.QueryString?



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-03-2014 11:40 PM

Hi, 

You can also use the ToDictionary() method:

var input = "Name=Irwin&Home=Caribbean&Preference=Coffee";

var items = input.Split(new[] { '&' });

var dict = items.Select(item => item.Split(new[] {'='})).ToDictionary(pair => pair[0], pair => pair[1]);


Don't want to miss updates? Please click the below button!

Follow MindStick