Kirsten
Kirsten

Reputation: 18036

Converting an Enum to a list

I have an enum defined as

public enum SecurityRole
{
    Admin = 0,
    ViewLog = 1,
    DeleteLog = 2
}

and want to use the technique described here to get a List<SecurityRole>.

Thus the line

var lst = Enum.GetValues(typeof(SecurityRole)).Cast<SecurityRole>().ToList();

should work, but IntelliSense wont allow the .ToList property

I am using System.Linq. Is there some other reference I need?

Upvotes: 0

Views: 126

Answers (2)

PSL
PSL

Reputation: 123739

using System.Linq should do it. ToList() is an IEnumerable<T> extension method. Probably check if you have any other compilation errors in your code.

Upvotes: 2

Ahmed Magdy
Ahmed Magdy

Reputation: 6030

Check the solution here: How do I convert an enum to a list in C#?

Enum.GetValues(typeof(SomeEnum)).Cast<SomeEnum>();

Upvotes: 2

Related Questions