Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 671
User999999 Avatar asked Sep 11 '14 06:09

User999999


2 Answers

You can use StringComparer to find keys ignoring their case:

var qStrings = new Dictionary<string, string>(
    HtmlPage.Document.QueryString,
    StringComparer.OrdinalIgnoreCase)
like image 166
takemyoxygen Avatar answered Oct 12 '22 20:10

takemyoxygen


Simplest Way

qStrings = qStrings .ToDictionary(k => k.Key.ToUpper(), k => k.Value.ToUpper());
like image 34
ANJYR Avatar answered Oct 12 '22 20:10

ANJYR