0

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.

5
  • 2
    Since the amount of months in one year doesn't change frequently, Something like staffMember.OctDays + staffMember.NovDays ... works. Of course there are ways like find out fields in StaffMember that end with Days and add them all, but if the table adds another column ends with Days in 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 in StaffMember or some sort of wrapper class. Commented Jul 21, 2022 at 8:38
  • 2
    I wouldn't recommend iterating over properties, as it always involves using reflection, and reflection can (and does) break, and when it does you have no idea why. You could add a SumDaysWorked method to your StaffMember class 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 code Commented Jul 21, 2022 at 8:41
  • You can do that using reflection but I wouldn't recommend this approach. Try to create computed property. It can be defined on db level or directly inside your model, whatever is best for you. Also it might worth considering denormalizing your schema and moving your days data to another table Commented Jul 21, 2022 at 8:42
  • 3
    I highly suggest your rework your DB schema, what happens if an employee is working for over a year, i.e the second say November? Do the days worked just get overwritten? Added together? What if want to know how many days that employee worked in November last year? Commented Jul 21, 2022 at 8:48
  • The question has nothing to do with iteration. If you want to sum some properties, do that. Just add OctDays + 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 Commented Jul 21, 2022 at 9:01

2 Answers 2

2

You can do it by iterating over object properties and apply your condition on it.

    static void Main(string[] args)
    {
        var staffA = new StaffMember();
        int totalWorkDays = 0;
        staffA.AprDays = 5;
        staffA.FebDays = 7;

        foreach (var item in staffA.GetType().GetProperties())
        {
            if (item.Name.EndsWith("Days"))
            {
                totalWorkDays += (int)item.GetValue(staffA)!;
            }
        }
        Console.WriteLine(totalWorkDays);
    }

this snippet prints ( 5 + 7 ) => 12

Sign up to request clarification or add additional context in comments.

2 Comments

As @mindswipe said, this is not the recommeded way I also highly suggest you rework your DB schema and use computed props.
Edit the answer to add extra information, don't post it as a comment
0

You can use reflection to iterate over the properties, but I do not recommend this because you have to point anyway which properties you want to take into consideration. That's because you have multiple integer properties like Id and Phone so you cannot indicate in the loop that you want to sum integer properties, you have to show explicitly that you want to sum OctDays etc. or write some algorithms that indicates that the current property is responsible for the month. So the best way (and in my opinion simplier than reflection way) would be just to get each of the month explicit and sum like this:

var sum = staffMember.OctDays + staffMember.NovDays + staffMember.DecDays + staffMember.JanDays + staffMember.FebDays + staffMember.MarDays + staffMember.AprDays 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.