How to use Enum's in a foreach loop where the Enum represents a property field and acts as counter (Months)?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to use Enum's in a foreach loop where the Enum represents a property field and acts as counter (Months)?



I need to generate some hour accounts if they don't exist and tried to make the method as easy as possible. Unfortunately I'm stuck at the following.



This is how I would check / create an account for every month:


// January

if (!_db.Stundenkonto.Any(x => x.MitarbeiterId == mitarbeiterId && x.Jahr == jahr && x.Monat == 1))

HrsAccount acc = new HrsAccount

AccountId = new Guid(),
UserId = loggedInUser,
Month = 1 <<--- This is where I'd need the number of the Enum - 1,
HoursToWork = model.data[0].january <<--- Here I'd need the name of the Enum
HoursWorked = model.data[1].january <<-- Same here




So as you can see I need number AND value of the Enum. I tried:


foreach (MonthEnum month in Enum.GetValues(typeof(MonthEnum)))

HrsAccount acc = new HrsAccount

AccountId = new Guid(),
UserId = loggedInUser,
Month = (int)month,
HoursToWork = model.data[0].month <<-- Gives errors
HoursWorked = model.data[1].month <<-- on these two




Is it possible to get this working as I need it?





What is model.data[0].january? Or what do you want it to be?
– Michał Turczyn
3 mins ago


model.data[0].january





Please show us the full type of model.data.
– Peter B
3 mins ago


model.data





Is MonthEnum an enum numbered from 1..12? And is model.Data an array indexed from 0..1?
– Harald Coppoolse
2 mins ago


MonthEnum




1 Answer
1



If I understand you correctly you need to get the name of the current enum value?
You can do this using https://msdn.microsoft.com/de-de/library/system.enum.getname(v=vs.110).aspx



Further you can use following to get the represented integer value:


var x = (int)MyEnum.AAAA;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard