我有一个应用程序对象,其中有Applicant对象。申请人对象有就业对象,其中包含有关就业(姓名,地址,...)的所有信息,可以是0或2,3,4,...我如何获得1个申请的职业点数?因此,在这种情况下,它不得不返回4.但是我的代码返回2,因为它是为每个申请人而不是应用程序返回计数。在C#中的孩子的小孩的返回计数
这是我的代码:
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; }
.....
}
考虑将application.NumberOfEmployments = item.Employments.Count();'application.NumberOfEmployments + = item.Employments.Count();'application.NumberOfEmployments从0开始,并且没有人会添加或删除来自它的物品。在所有其他情况下,您可以考虑将其作为生命计数,并且每次询问对象时只需迭代集合,或者使用ObservableCollections并通过INotifyCollectionChanged事件跟踪更改 – Icepickle