I was trying to make my code cleaner by moving something that I call a lot into a static method. The original code is:
List<ListItem> listItems = Enum
.GetValues(typeof(TimelineinfoEnums.StateEnum))
.Cast<TimelineinfoEnums.StateEnum>()
.Select(x => new ListItem {
Text = x.ToString(),
Value = (int)x })
.ToList();
TimelineinfoEnums.StateEnum
is just a place holder, the point of my method is to be able to call it, pass in a value, and make TimelineinfoEnums.StateEnum
some other enum that I'm working with. After reading elsewhere on SO, that you have to use a string input for things like this because the "entity type is literally not known, or knowable, at compile time.", I tried this code:
public static List<ListItem> GetDropdownList(string @enum)
{
List<ListItem> listItems = Enum
.GetValues(@enum.GetType())
.Cast<@enum.GetType()>()
.Select(
x => new ListItem { Text = x.ToString(), Value = (int)x }).ToList();
return listItems;
}
However, when I attempted to do so, on the second @enum.GetType()
I got an error of:
Operator '<' cannot be applied to operands of type 'method group' and 'Type'
I've tried removing the .GetType()
bit but then I get an error of
enum is a variable but is used like a type
I'm not too familar with Linq, so I'm likely missing something straightforward, thanks for any help!
Edit: ListItem
Looks like this:
public class ListItem
{
public int Value { get; set; }
public string Text { get; set; }
}