CONVERT QUERY STRING TO KEY-VALUE PAIR

Jeet Verma

Total Post:110

Points:776
Posted by  Jeet Verma
 1156  View(s)
Ratings:
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?

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Convert query string to key-value pair

    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]);

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!