2016-02-25 67 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(); 
     } 
    }