Ответ 1
Вы можете использовать LINQ. Он будет выглядеть примерно так:
int averageAge = this.DataWorkspace.ApplicationData.InsuranceQuotations.
Where(iq => iq.Gender == Gender.Female).
Average(iq => iq.mAge);
Я хотел бы получить пол для расчетов, например, мужские и женские варианты находятся в одной колонке. Я хотел бы получить всех мужчин или всех женщин для расчета.
У меня есть "вычисленное свойство", которое дает мне список всех элементов вместе с вычислением. Вот код:
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();