I basically have an enumeration
public enum WorkingDays
{
Monday, Tuesday, Wednesday, Thursday, Friday
}
and would like to make a comparison with the input, which is a string
string input = "monday";
The best I could come up with was something like this
WorkingDays day = (from d in Enum.GetValues(typeof(WorkingDays)).Cast<WorkingDays>()
where d.ToString().ToLowerInvariant() == input.ToLowerInvariant()
select d).FirstOrDefault();
Is there a better way to do this?
Edit: Thanks to Aaron and Jason. But what if parsing fails?
if(Enum.IsDefined(typeof(WorkingDay),input))//cannot compare if case is different
{
WorkingDay day = (WorkingDay)Enum.Parse(typeof(WorkingDay), input, true);
Console.WriteLine(day);
}
source
share