Enumerate through the values of a enum
-
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()
andEnum.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.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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.
A guide to posting questions on CodeProject[^]
Dave Kreskowiakand 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)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 becauseArray
implements the non-genericIEnumerable
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)
-
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)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 becauseArray
implements the non-genericIEnumerable
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 becauseArray
implements the non-genericIEnumerable
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...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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 becauseArray
implements the non-genericIEnumerable
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...
A guide to posting questions on CodeProject[^]
Dave KreskowiakFirst 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)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 ofArray
(the base class), and not a instance ofint[]
.int[]
implementsIEnumerable<int>
, whileArray
implementsIEnumerable
, theSelect
method ExpectsIEnumerable<TSource>
... while writting this post, i updated this bit of code to this:ViewBag.ApetiteOptions = Enum.GetValues(typeof(Apetite)).OfType().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)
-
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)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 ofArray
(the base class), and not a instance ofint[]
.int[]
implementsIEnumerable<int>
, whileArray
implementsIEnumerable
, theSelect
method ExpectsIEnumerable<TSource>
... while writting this post, i updated this bit of code to this:ViewBag.ApetiteOptions = Enum.GetValues(typeof(Apetite)).OfType().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)
-
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()
andEnum.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)
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() to coerce it to a generic IEnumerablevar list = Enum.GetValues(typeof(States))
.OfType()
.Select(i => .... );Live example: http://rextester.com/TRHMMB31708[^]
-
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 becauseArray
implements the non-genericIEnumerable
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...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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)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 ofArray
(the base class), and not a instance ofint[]
.int[]
implementsIEnumerable<int>
, whileArray
implementsIEnumerable
, theSelect
method ExpectsIEnumerable<TSource>
... while writting this post, i updated this bit of code to this:ViewBag.ApetiteOptions = Enum.GetValues(typeof(Apetite)).OfType().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)
-
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
Crap. That's what I get with 4 hours of sleep... :-\
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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() to coerce it to a generic IEnumerablevar list = Enum.GetValues(typeof(States))
.OfType()
.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! :-D
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)