我有一个应用程序对象,其中包含申请者对象。申请者对象有就业对象,该对象包含有关就业的所有信息(名称、地址、.)可以是0或者2,3,4,.我怎样才能计算出一份申请的电子邮件数量?所以在这个例子中,它必须返回4,但是我的代码返回2,因为它返回每个申请者的计数,而不是申请。

这是我的密码:
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; }
....
}这就是就业对象:
public class Employment
{
public int Id { get; set; }
public string EmployerName { get; set; }
public string EmployerPhoneNumber { get; set; }
public Applicant Applicant { get; set; }
.....
}发布于 2016-02-25 20:30:45
要获得总就业人数,请考虑代码中的以下更改:
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,因为这是上一个应用程序的雇用人数。
发布于 2016-02-25 20:24:25
如果我正确地理解了您的结构,您应该能够使用相当简单的SelectMany和Count
application.Applicants.SelectMany(a => a.Employments).Count();发布于 2016-02-25 20:23:31
好吧,你已经快到了,问题是你要替换每个子节点的值,而不是添加它。
改为:
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);
}https://stackoverflow.com/questions/35637474
复制相似问题