I have this enum:
public enum Role {
Admin,
Manager,
Engineer,
User
}
How could I project this into anonymous type, which I would send as JSON from a ASP.NET endpoint:
{
{ id: 0, name: "Admin" },
{ id: 1, name: "Manager" },
{ id: 2, name: "Engineer" },
{ id: 3, name: "User" }
}
How could I achieve this?
You can try this:
var result=Enum.GetNames(typeof(Role)).Select((e, i) => new { id = i, name = e });
Enum.GetNames
return an string[]
with the values of enum and Select
extension method has an everload that gives you also the index