I have a table "StaffMembers" that have columns indicating the number of days worked in a month, the properties in the model are as follows:
public class StaffMember
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public int Phone { get; set; }
public string Email { get; set; }
public string BirthDate { get; set; }
public int OctDays { get; set; }
public int NovDays { get; set; }
public int DecDays { get; set; }
public int JanDays { get; set; }
public int FebDays { get; set; }
public int MarDays { get; set; }
public int AprDays { get; set; }
}
now I retrieve the specific staffMember using linq:
var staffMember = (from b in db.StaffMembers
where b.Id == Id
select b).FirstOrDefault();
what I want to do is to loop over the months properties in staffMember and add all the worked days together to get total working days in the year. for example if he worked 10 days in oct and 20 days in dec and 30 days in jan, I want a way to iterate over the months and sum the days.
staffMember.OctDays + staffMember.NovDays ...works. Of course there are ways like find out fields inStaffMemberthat end withDaysand add them all, but if the table adds another column ends withDaysin the future, this method will not sustain. Add them all may sound stupid, but it's easily readable and works. If you need to re-use it in other places, you can wrap it into a method - maybe even write that logic inStaffMemberor some sort of wrapper class.SumDaysWorkedmethod to yourStaffMemberclass and then have to just define it once. Or, better yet, re-structure your DB so you have a relation to a table where the days worked are stored in and you can write a clever query to fetch it on the DB instead of loading every entry and calculating in your client codeOctDays + NovDays + .... That's not a very flexible design though. It assumes each member works for less than 1 year. The solution isn't using reflection, it's using a well designed table (or view?) schema that matches the requirements