【问题标题】:How to get all the females?如何获得所有女性?
【发布时间】:2015-08-04 22:16:52
【问题描述】:

我想获取性别进行计算,例如男性和女性选项在一列中。我想得到所有男性或所有女性进行计算。

我有一个“计算属性”,它提供了所有项目的列表以及计算。这是代码:

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

}

如何过滤此“计算属性”中的性别。

【问题讨论】:

  • 计算属性不给出循环。什么意思?
  • 我什至对只有一个的非泛型算法感兴趣。谢谢。
  • 你能用 if 语句过滤吗?
  • 与您的问题无关,但您为什么要传递 ref string 参数而不是返回字符串值?为什么返回一个字符串而不是一个数字?
  • 我无法回答你的问题,所以我在这里重新发布了它 - social.msdn.microsoft.com/Forums/vstudio/en-US/…

标签: c# visual-studio-lightswitch lightswitch-2013 lightswitch-2012


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我不喜欢if (value == true),用LINQ会更好。
    【解决方案3】:

    希望这可以帮助其他人过滤选择列表 在 _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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 2021-11-26
      • 2018-10-18
      • 1970-01-01
      相关资源
      最近更新 更多