Convert query string to key-value pair

Total Post:110

Points:776
 1431  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. 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]);

      Modified On Apr-05-2018 05:38:49 AM

Answer