26

Chciałbym uzyskać płeć do obliczeń, na przykład opcje męskie i żeńskie znajdują się w jednej kolumnie. Chciałabym załatwić wszystkich mężczyzn lub wszystkie kobiety do obliczenia.Jak zdobyć wszystkie kobiety?

Mam "obliczoną właściwość", która daje mi listę wszystkich elementów wraz z obliczeniem. Oto kod:

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

} 

Jak mogę filtrować płeć w tej "wyliczonej nieruchomości".

+0

Obliczone właściwości nie dają pętle. Co masz na myśli? –

+35

Byłbym zainteresowany nawet nietypowym algorytmem, który uzyskuje tylko jeden. Dzięki. –

+0

Czy można filtrować za pomocą instrukcji if? – jarchuleta

Odpowiedz

0

Czy można filtrować za pomocą instrukcji 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(); 
    } 

} 
+0

Nie lubię 'if (value == true)', a użycie LINQ byłoby lepsze. – Hosch250

7

Możesz użyć LINQ. Byłoby to wyglądać mniej więcej tak:

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

Nadzieja pomogłoby kogoś do filtrowania listy wyboru w _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(); 

Albo

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(); 
Powiązane problemy