|
ok, I Know about Enum.GetValues() , but what i want to know if is possible is to do a foreach through the values of a enum such as i can do the following:
foreach(var value in enum){
list.Add(new SelectListItem(){
Text = value.ToString(),
Value = ((int)value).ToString()
});
}
say that i have the following enum:
enum States{
Running,
Stoped,
Paused
}
the only way to accomplish what i described above is through the use of Enum.GetValues() and Enum.GetNames() ? or there is some syntax sugar?
I'm asking more out of curiosity than anything else
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
Nope. No syntactic sugar. Since an Enum is not enumerable, you can't just specify the enum type itself. You must get the enum values in an array or other IEnumerable implementor first.
|
|
|
|
|
and the only way to do this and maintain the type of the enum is adding the values manually to the list...
I was thinking of someway to call a method with every possibly constant of a enum, but the only thing i got so far is something like:
foreach(int value in Enum.GetValues(typeof(MyEnum))){
MyEnum e = (MyEnum)value;
...
}
or if you prefer the linq way...
((IEnumerable<int>)Enum.GetValues(typeof(MyEnum))).Select(x => (MyEnum)x);
the only thing i don't understand is why i can't call the Select method directly on the return of Enum.GetValues() , maybe because Array implements the non-generic IEnumerable interface?
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
Sentenryu wrote: the only thing i don't understand is why i can't call the Select method directly
on the return of Enum.GetValues() , maybe because Array
implements the non-generic IEnumerable interface?
LINQ works on the IQueryable interface, which array's don't support.
Arrays support the following interfaces:
ICloneable, IList, ICollection, IEnumerable, IStructuralComparable and IStructuralEquatable
The .Select extension works on any IEnumerable, including arrays, so I think your .Select example is a bit flawed. I don't know what you're doing, but...
|
|
|
|
|
First of all, i forgot to say thanks for your answer, so Thanks!
In the case i'm just getting a IEnumerable<SelectListItem> with the values of a enum to display as radiobuttons, the actual code (with the excuse of having names in portuguese) is this:
ViewBag.ApetiteOptions = ((IEnumerable<int>)Enum.GetValues(typeof(Apetite))).Select(x => new SelectListItem() {
Text = ((Apetite)x).ToString(),
Value = x.ToString()
});
Visual Studio (2012) gives me an error when I try to use the select method directly on the return type of the
Enum.GetValues() , I guess it's because this method returns a instace of Array (the base class), and not a instance of int[] .
int[] implements IEnumerable<int> , while Array implements IEnumerable , the Select method Expects IEnumerable<TSource> ...
while writting this post, i updated this bit of code to this:
ViewBag.ApetiteOptions = Enum.GetValues(typeof(Apetite)).OfType<int>().Select(x => new SelectListItem() {
Text = ((Apetite)x).ToString(),
Value = x.ToString()
});
I'm a little bit more happy now
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
|
See my answer below, all your LINQ example is missing is a call to
OfType<States>()
|
|
|
|
|
Dave Kreskowiak wrote: LINQ works on the IQueryable interface, which array's don't support.
No, LINQ to SQL / EF works on IQueryable, most of Linq2Objects works on IEnumerable<t>
|
|
|
|
|
Crap. That's what I get with 4 hours of sleep...
|
|
|
|
|
GetValues() does return an enumerable list of the actual enums, so your original code works with VERY few changes:
foreach(var value in Enum.GetValues(typeof(States))){
list.Add(new SelectListItem(){
Text = value.ToString(),
Value = ((int)value).ToString()
});
}
Live example: http://rextester.com/PWOX45762[^]
You can also use LINQ if you want, but that call to Enum.GetValues() returns an Array, so you neeed to call OfType<states>() to coerce it to a generic IEnumerable<states>
var list = Enum.GetValues(typeof(States))
.OfType<States>()
.Select(i => .... );
Live example: http://rextester.com/TRHMMB31708[^]
|
|
|
|
|
Wow, I always thought it returned a Array containing int's, this will really simplify the things! thanks a lot!
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|