我想获得用于计算的性别,例如男性和女性选项在一列中。我想让所有男性或所有女性进行计算。
我有一个“计算属性”,它给出了所有项目的列表以及计算。这是代码:
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();
}
}
如何在此“计算属性”中过滤性别。
答案 0 :(得分:7)
您可以使用LINQ。它看起来像这样:
int averageAge = this.DataWorkspace.ApplicationData.InsuranceQuotations.
Where(iq => iq.Gender == Gender.Female).
Average(iq => iq.mAge);
答案 1 :(得分: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();
}
}
答案 2 :(得分:0)
希望这可以帮助其他人过滤选择列表 在_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();