Eric Saboia Eric Saboia - 2 months ago 29
ASP.NET (C#) Question

Verify if contains Request.QueryString in enum list

How Can I verify if contains Request.QueryString in enum list?
E.g.:
I have a

enum
:

enum queryString
{
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday
}


Get enum values:

var enumQueryString = Enum.GetValues(typeof(queryString)).Cast<queryString>().ToList();


I tried

Request.QueryString["days"].Contains(enumQueryString.ToString())


or

for (int i = 0; i < enumQueryString.Count; i++)
if (Request.QueryString["days"].Contains(enumQueryString[i].ToString()))
Response.Redirect("~/Default.aspx");


But in the last case don't work correctly, because of iteration.

Answer

It's the other way around: you should check enumQueryString list:

var enumQueryStringList = Enum.GetValues(typeof(queryString)).Cast<queryString>().ToList();
if (enumQueryStringList.Contains(Request.QueryString["days"])) {
    ...
}

You can do it without LINQ, too:

queryString qs;
if (Enum.TryParse<queryString>(Request.QueryString["days"], out qs)) {
    ...
}