Alma
Alma

Reputation: 4390

Return count of the child of child in C#

I have a Application Object that has Applicant object in it. Applicant objects has Employments Object that has all information about the Employment (name,address,...) this can be 0 or 2,3,4,... How I can get the Count of Emp for 1 Application? So in this case in picture it had to return 4. But my code is returning 2 as it is returning the count for each Applicant not Application.

enter image description here

This is my code:

 public void InsertApplication(Application application)
    {
        if (application.Applicants != null)
        {
            foreach (var item in application.Applicants)
            {
                if (item.Employments != null)
                {
                    application.NumberOfEmployments = item.Employments.Count();
                }
            }
        }
        creditApplicationsContext.Applications.Add(application);
    }

This is Application Object:

  public class Application
    {
      public int Id { get; set; }
      public virtual ICollection<Applicant.Applicant> Applicants { get; set;    }
      public int? NumberOfEmployments { get; set; }

This is Applicant object:

 public class Applicant
{

    public int Id { get; set; }
    public virtual ICollection<Employment> Employments { get; set; }
      ....
     }

and this is Employment Object:

  public class Employment
  {

    public int Id { get; set; }
    public string EmployerName { get; set; }
    public string EmployerPhoneNumber { get; set; }
    public Applicant Applicant { get; set; } 
     .....
    }

Upvotes: 1

Views: 337

Answers (6)

Claudio
Claudio

Reputation: 652

To get the total number of Number of employments consider this change in your code:

public void InsertApplication(Application application)
{
    if (application.Applicants != null)
    {
        foreach (var item in application.Applicants)
        {
            if (item.Employments != null)
            {
                application.NumberOfEmployments += item.Employments.Count();
            }
        }
    }
    creditApplicationsContext.Applications.Add(application);
}

In this way you have the total number of employments. I think your code return 2 because is the number of the employments of the last application.

Upvotes: 1

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

You can add a method to Get number of applications and use SelectMany

 public class Application
    {
        public int Id { get; set; }
        public virtual ICollection<Applicant> Applicants { get; set; }
        public int? NumberOfEmployments { get; set; }

        public int GetNumberOfApplications()
        {
            return this.Applicants.SelectMany(x => x.Employments).Count();
        }
    }

Upvotes: 2

Martin Staufcik
Martin Staufcik

Reputation: 9490

SelectMany could help:

application.Applicants.SelectMany(x => x.Employments).Count();

Upvotes: 2

Justin Niessner
Justin Niessner

Reputation: 245429

If I'm understanding your structure properly, you should be able to use a fairly simple SelectMany and Count:

application.Applicants.SelectMany(a => a.Employments).Count();

Upvotes: 5

Gusman
Gusman

Reputation: 15151

Well, you were nearly there, the problem is you're replacing the value for each child node, instead of adding it.

Change to this:

public void InsertApplication(Application application)
{
    application.NumberOfEmployments = 0;

    if (application.Applicants != null)
    {
        foreach (var item in application.Applicants)
        {
            if (item.Employments != null)
            {
                application.NumberOfEmployments += item.Employments.Count();
            }
        }
    }
    creditApplicationsContext.Applications.Add(application);
}

Upvotes: 2

Raidri
Raidri

Reputation: 17550

application.NumberOfEmployments =
    application.Sum(app1 => app1.Applicants.Sum(app2 => app2.Employments.Count()));

Upvotes: 1

Related Questions