Click here to Skip to main content
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 11: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></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 200
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 10 Oct 2011
Copyright © CodeProject, 1999-2014
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