Search code examples
c#idictionaryquerystringparameter

Cast all keys in dictionary to uppercase


This is probably a very simple question but google has let me down sofar and keeps pointing me towards python solutions.

I have a webpage where applciations/users can supply querystringparameters.To Retrieve the querystring parameters I use the following code:

IDictionary<string, string> qStrings = HtmlPage.Document.QueryString;

to check the presence of a specified key, I use the following code:

if (!String.IsNullOrEmpty(qStrings["PARAM1"]))
{}

Knowing our users, i'm expecting them to give parameterkeys as follows: "Param1", "param1", "pArAm1" How can simply cast every key in a dictionary to uppercase without iterating each key-valuepair?

Or how can i alter the qStrings["PARAM1"] so it ignores the case?


Solution

  • You can use StringComparer to find keys ignoring their case:

    var qStrings = new Dictionary<string, string>(
        HtmlPage.Document.QueryString,
        StringComparer.OrdinalIgnoreCase)