2016-02-25 50 views
1

我有一个应用程序对象,其中有Applicant对象。申请人对象有就业对象,其中包含有关就业(姓名,地址,...)的所有信息,可以是0或2,3,4,...我如何获得1个申请的职业点数?因此,在这种情况下,它不得不返回4.但是我的代码返回2,因为它是为每个申请人而不是应用程序返回计数。在C#中的孩子的小孩的返回计数

enter image description here

这是我的代码:

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

这是适用对象:

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

这是申请对象:

public class Applicant 
{ 

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

,这是EMPL oyment对象:

public class Employment 
    { 

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

考虑将application.NumberOfEmployments = item.Employments.Count();'application.NumberOfEmployments + = item.Employments.Count();'application.NumberOfEmployments从0开始,并且没有人会添加或删除来自它的物品。在所有其他情况下,您可以考虑将其作为生命计数,并且每次询问对象时只需迭代集合,或者使用ObservableCollections并通过INotifyCollectionChanged事件跟踪更改 – Icepickle

回答

1

要获得数总数的就业考虑在你的代码这一变化:

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

这样,你有就业的总人数。 我认为你的代码返回2,因为是最后一个应用程序的就业人数。

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

嗯,你几乎没有,问题是你要替换的值,每个子节点,而不是添加它。

改成这样:

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

如果我正确理解你的结构,你应该能够使用相当简单SelectManyCount

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

SelectMany可以帮助:

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

您可以添加一个方法来获取数字应用程序和使用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(); 
     } 
    }