Reputation: 40062
I have a method such as:
public void DoIt(params MyEnum[] channels)
{
}
Is there a way to get the int values of the enums in the params?
I tried var myInts = channels.Select(x => x.Value)
but no luck
Upvotes: 2
Views: 240
Reputation: 1063964
Another way to do the same, without LINQ:
var vals = Array.ConvertAll(channels, c =>(int)c);
Upvotes: 5
Reputation: 48124
var myInts = channels.Select(x=x.Value)
this doesn't work because =
needs to be => (int)x
I think var myInts = channels.Select(x => (int)x).ToArray();
will do what you want.
Upvotes: 2
Reputation: 91696
You could do:
var myInts = channels.Select(e => (int)e);
Or as a LINQ query:
var myInts = from e in channels select (int)e;
You could then call ToArray()
or ToList()
on myInts
to get a MyEnum[]
or a List<MyEnum>
Upvotes: 1
Reputation: 19465
This should do it:
public void DoIt(params MyEnum[] channels)
{
int[] ret = channels.Select(x => ((int)x)).ToArray();
}
Upvotes: 1
Reputation: 17603
You can do a cast in the select clause:
var myInts = channels.Select(x => (int)x);
Upvotes: 5