简体   繁体   English

迭代实体框架对象属性

[英]Iterate over Entity Framework object properties

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:我有一个表“StaffMembers”,其中的列表示一个月的工作天数,模型中的属性如下:

    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:现在我使用linq检索特定的staffMember:

                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.我想要做的是遍历staffMember中的month属性并将所有工作日加在一起以获得一年中的总工作日。 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.例如,如果他在 10 月工作了 10 天,在 12 月工作了 20 天,在 1 月工作了 30 天,我想要一种方法来迭代几个月并总结这些天数。

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此代码段打印 (5 + 7) => 12

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.那是因为你有多个整数属性,比如 Id 和 Phone,所以你不能在循环中指出你想要对整数属性求和,你必须明确表明你想要对 OctDays 等进行求和,或者编写一些算法来表明当前属性是负责当月。 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM