如何让所有的女性?

Xdr*_*one 27 c# visual-studio-lightswitch lightswitch-2012 lightswitch-2013

我想得到性别用于计算,例如男性和女性选项在一列中.我想让所有男性或所有女性进行计算.

我有一个"计算属性",它给出了所有项目的列表以及计算.这是代码:

partial void MeanFemale_Compute(ref string result)
{
    // Set result to the desired field value

    int totalAge = 0;
    int count = 0;

    foreach (InsuranceQuotation i in his.DataWorkspace.ApplicationData.InsuranceQuotations)
    {
        totalAge += i.mAge;
        count++;
    }

    if (count != 0)
    {
        result = (totalAge / count).ToString();
    }

}
Run Code Online (Sandbox Code Playgroud)

如何在此"计算属性"中过滤性别.

小智 7

您可以使用LINQ.它看起来像这样:

int averageAge =  this.DataWorkspace.ApplicationData.InsuranceQuotations.
    Where(iq => iq.Gender == Gender.Female).
    Average(iq => iq.mAge);
Run Code Online (Sandbox Code Playgroud)


jar*_*eta 0

你能用 if 语句过滤吗?

partial void MeanFemale_Compute(ref string result)
{
    // Set result to the desired field value

    int totalAge = 0;
    int count = 0;

    foreach (InsuranceQuotation i in this.DataWorkspace.ApplicationData.InsuranceQuotations)
    {

        if(i.Female == true)
        {
            totalAge += i.mAge;
            count++;
        }
    }

    if (count != 0)
    {
        result = (totalAge / count).ToString();
    }

}
Run Code Online (Sandbox Code Playgroud)