在ASP.NET CORE
中开发应用程序时遇到了以下问题:
我有DBSet<UserHistory>
。我需要做的是访问UserHistory中的元素Sessions。然后我需要从session元素中访问Jobs里面的最后一个元素。
理想情况下,我想加载一个元素而不加载所有集合。
我尝试了以下操作但它不起作用:
...
var item = userHistory.Where(n=>Name == name).Include(s=>s.Sessions).Last().
ThenInclude(j=>j.Jobs).Last();
非常感谢任何帮助。谢谢!
public class UserHistory
{
public int Id { get; set; }
public string UserId { get; set; }
public List<SessionHistory> Sessions { get; set; }
public UserHistory()
{
}
}
public class SessionHistory
{
public int Id { get; set; }
public DateTime ClockIn { get; set; }
public DateTime ClockOut { get; set; }
public List<JobHistory> Jobs { get; set; }
// The flaged must be set when the user clocks out
public bool IsClockedOut { get; set; }
public SessionHistory()
{
Jobs = new List<JobHistory>();
}
}
public class JobHistory
{
public int Id { get; set; }
public string CompanyName { get; set; }
public int JobNumber { get; set; }
public string JobDescription { get; set; }
public DateTime JobStart { get; set; }
public DateTime JobFinish { get; set; }
// Will be set to true when signed out of the job
// Use it to determain if the the user is signed out!
public bool IsJobComplete{ get; set; }
}
答案 0 :(得分:0)
首先:
此部分(n=>Name == name)
应为(n=>n.Name == name)
,不是吗?
然后尝试类似的事情:
var item = userHistory.Where(n=>n.Name == name).First().Sessions.Last().Jobs.Last();