如何得到所有的雌性

本文关键字:何得 | 更新日期: 2023-09-27 18:06:27

我想获得用于计算的性别,例如,男性和女性选项在一列中。我想用全男性或全女性来计算。

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

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();
    }
}

如何在这个"计算属性"中过滤性别

如何得到所有的雌性

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

int averageAge =  this.DataWorkspace.ApplicationData.InsuranceQuotations.
    Where(iq => iq.Gender == Gender.Female).
    Average(iq => iq.mAge);

是否可以使用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();
    }
}

希望这将有助于其他人筛选选择列表在_InitializeDataWorkspace:

        // get count of females
        double fgender = (from gender in InsuranceQuotations
                             where gender.mGender == "Female"
                             select gender).Count();
        //get sum of females ages
        double female = InsuranceQuotations.Where(x => x.mGender == "Female").Sum(t => t.mAge);
        // get count males
        double mgender = (from gender in InsuranceQuotations
                             where gender.mGender == "Male"
                             select gender).Count();
        //get sum of males ages
        double male = InsuranceQuotations.Where(x => x.mGender == "Male").Sum(t => t.mAge);     
        // MeanFmale amd MeanMmale - The fields that display 
        MeanFmale = (female / fgender).ToString();
        MeanMmale = (male / mgender).ToString();

   double fmale = InsuranceQuotations.Where(x => x.mGender == "Female").Average(t => t.mAge);
   double mmale = InsuranceQuotations.Where(x => x.mGender == "Male").Average(t => t.mAge);
    // MeanFmale amd MeanMmale - The fields that display 
    MeanFmale = fmale.ToString();
    MeanMmale = mmale.ToString();