27

I would like to get the gender for calculations, for example the male and female options are in one column. I would like to get all male or all female for calculation.

I have a "computed property" which gives me list of all the items along with calculation. Here is the code:

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

}

How do I get to filter the gender in this "computed property".

user703016
  • 37,307
  • 8
  • 87
  • 112
Xdrone
  • 781
  • 1
  • 11
  • 21
  • Computed properties do not give loops. What do you mean? – Andrew Savinykh Aug 04 '15 at 22:24
  • 35
    I'd be interested even in a non-generic algorithm that only gets one. Thanks. –  Aug 04 '15 at 23:07
  • Could you filter with an if statement? – jarchuleta Aug 04 '15 at 23:46
  • Not related to your question, but why are you passing a `ref string` parameter rather than returning a string value? And why return a string and not a number? – D Stanley Aug 12 '15 at 15:40
  • I could not answer your question so I reposted it here - https://social.msdn.microsoft.com/Forums/vstudio/en-US/08787015-43d6-493f-bc5d-01ad6756c742/ref-string-result?forum=lightswitch – Xdrone Aug 13 '15 at 08:49

3 Answers3

7

You can use LINQ. It would look something like this:

int averageAge =  this.DataWorkspace.ApplicationData.InsuranceQuotations.
    Where(iq => iq.Gender == Gender.Female).
    Average(iq => iq.mAge);
DLCross
  • 699
  • 5
  • 13
0

Could you filter with an if statement?

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

}
jarchuleta
  • 1,231
  • 8
  • 10
0

Hope this would help someone else for filtering the Choice List in _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();

Or

   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();
Xdrone
  • 781
  • 1
  • 11
  • 21