5

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?

User999999
  • 2,500
  • 7
  • 37
  • 63
  • 2
    Don't do `!String.IsNullOrEmpty(qStrings["PARAM1"])`, that will crash if `PARAM1` doesn't exist. What you should do is `if(qStrings.TryGetValue("PARAM1", out param1)) { ... }` – Alxandr Sep 11 '14 at 07:02
  • @Alxandr, thanks, you just saved me a whole lot of support :) – User999999 Sep 11 '14 at 07:09
  • Do note that `param1` might be `null` or empty string still though, so you might want to check that inside the `{ ... }` – Alxandr Sep 11 '14 at 07:13

4 Answers4

9

You can use StringComparer to find keys ignoring their case:

var qStrings = new Dictionary<string, string>(
    HtmlPage.Document.QueryString,
    StringComparer.OrdinalIgnoreCase)
takemyoxygen
  • 4,294
  • 22
  • 19
  • 3
    Heads up that this will fail if you have both `param1` and `ParAM1` in the query string. You might want to guard against that somehow. – Alxandr Sep 11 '14 at 07:15
  • @Alxandr, Again you make a very good point. i've added a check for that to my code-behind! – User999999 Sep 11 '14 at 07:19
2

Simplest Way

qStrings = qStrings .ToDictionary(k => k.Key.ToUpper(), k => k.Value.ToUpper());
ANJYR
  • 2,583
  • 6
  • 39
  • 60
1

Maybe you can do it like below:

Dictionary<string, string> qStrings = new Dictionary<string, string>();
foreach (var a in qStrings.Keys)
{
    switch (a.ToUpper())
    {
        case "PARAM1":
            break;
    }
}
cck3rry
  • 191
  • 2
  • Your solution could/should work (haven't tested it). But takemyoxygen solutions lies more in line with the existing code (+1 though) – User999999 Sep 11 '14 at 08:10
-1

Without iterating is not possible. No matter what approach you use there is going to be some sort of iteration. The this is you need to limit the insertion of the data to a single unified casing rather than allowing users to input all sorts of casing.

Taking your example: "Param1", "param1", "pArAm1", a key will be created for each single one of these as they are treated as separate entities. The best way to handle that is to force the casing at the insertion rather than when querying for values.

For example:

void AddToDictionary(string key, string value)
{
   qStrings[key.ToUpper()] = value;
}
Zaid Amir
  • 4,727
  • 6
  • 52
  • 101
  • As pointed out in another answer (http://stackoverflow.com/a/25781046/336648), you can ignore case when looking up keys in the dictionary. – Kjartan Sep 11 '14 at 07:08
  • @Kjartan but that will only impose a problem as you will have multiple entries with the same key but different casing – Zaid Amir Sep 11 '14 at 07:24
  • 1
    @RedSerpent: Just checked it. Even with different casing an exception is thrown when adding twice the same key. so no problems there :) – User999999 Sep 11 '14 at 07:36
  • @N55PEC Is right. albeit with one gotcha to be aware of: Assume you init a Dictionary B from the values of another Dictionary A, and B is set to ignore case, but A was not. Then A could contain items with keys that would be duplicates in B because of the differences in casing; this would result in an argument exception when initializing B, due to duplicate keys. This should not be a problem in your case though, _unless_ someone passes two query string parameters that only differ in casing - in which case an exception would be thrown when copying from `HtmlPage.Document.QueryString`. – Kjartan Sep 11 '14 at 07:58