Click here to Skip to main content
11,645,893 members (77,194 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: LINQ C#3.5 string
Okay, so I work for a "soda pop company," not really but to protect proprietary info, let's pretend I do. And I have an ASPX page that is passed a querystring like

http://www.sodaforu.com/page.aspx?brand=coke

OK, so I want to place an "if" in the code so that if I don't pass one of our soda brands in, I won't do anything for instance, so you can't put malicious code in the query string.

So I want to say, "if the brand passed is either blank or not one of a certain set of predefined specific values, then blah blah blah"

The guy who came before me wrote it this way, which is legal but he had about 100 different space-separated names in the string literal which makes it run off the edge of the VS window.

if (string.IsNullOrWhiteSpace(qsBrand) || !"coke pepsi sprite fanta barqs welchs 7-up A&W shasta dads pepsi drpepper".Contains(qsBrand))
{
    // whatever...
}

Is there any "more readable" way to code this? Maybe using LINQ/Deferred Execution?

Brian
Posted 10-Oct-11 10:04am
Comments
SAKryukov at 10-Oct-11 16:09pm
   
Problem not quite clear. Who cares about the edge of VS window, really?
What prevents you from using HTTP requests? AJAX?
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Though not most efficient, long string method works perfectly well. I'd declare a constant set of strings, like this:
private static readonly HashSet<string> LegalNames = new HashSet<string>(new[] {
    "coke"
,   "pepsi", ...
});</string>
and then use it to check the incoming queries, like this:
if (string.IsNullOrWhiteSpace(qsBrand) || !LegalNames.Contains(qsBrand)) {...}
Given a relatively small number of items on the list, we're talking a matter of personal preference: the tiny efficiency gain is not worth the additional typing.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 628
1 jyo.net 514
2 Sergey Alexandrovich Kryukov 478
3 Afzaal Ahmad Zeeshan 378
4 DamithSL 260
0 OriginalGriff 1,143
1 jyo.net 994
2 Sergey Alexandrovich Kryukov 808
3 Afzaal Ahmad Zeeshan 702
4 DamithSL 691


Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 10 Oct 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100