I need to convert a list of int
values to list of enum
values and vice versa. I can create a simple conversion method but wanted to see if there’s a more concise way to handle this.
Here’s what my enum values look like:
public enum AutoShipOption
{
Monthly = 2,
Weekly = 1,
Yearly = 3,
Undefined = 0
}
And in my class that I use to define a store item, I get a List<int>
that I need to convert to List<AutoShipOption>
. As I said, it’s easy to create a simple method that handles this that looks like this:
public List<AutoShipOption> AutoShipOptionsMapper(List<int> values)
{
var options = new List<AutoShipOption>();
if(values == null || values.Count == 0)
return options;
foreach(var item in values)
if(item > 0)
options.Add((AutoShipOption)item);
return options;
}
Is there a more concise way to handle this conversion?