我有一个具有ticketNotes(0到多个)的票证实体,并希望在查询票证时撤回创建每个票据的用户详细信息.
我使用以下代码查询故障单
var ticket = (from t in context.Tickets
.Include(t=>t.Site)
.Include(t=>t.Caller)
.Include(t=>t.Caller.Site)
.Include(t => t.Notes)
.Include(t=>t.OpenedByUser)
select t).First(t => t.TicketId == ticketId);
我的TicketNote类是:
public class TicketNote
{
public Guid TicketNoteId { get; set; }
public string NoteText { get; set; }
public DateTime CreatedTime { get; set; }
public Guid TicketId { get; set; }
public virtual Ticket Ticket { get; set; }
public bool ReadOnly { get; set; }
public DateTime? DateTimeDeleted { get; set; }
public Guid TenantId { get; set; }
[Required]
[DefaultValue(typeof(Guid), "00000000-0000-0000-0000-000000000000")]
public Guid CreatedByUserId { get; set; }
[ForeignKey("CreatedByUserId")]
public virtual User CreatedByUser { get; set; }
}
我想补充一下
.Include(t => t.Notes.CreatedByUser)
但是,由于笔记是一个集合,我没有得到选项.
请告知最佳方法,以实现撤回当前为NULL的CreatedByUser.
谢谢
最佳答案 您可以包含更复杂的语句来获取多级集合.请尝试以下方法:
.Include(t => t.Notes.Select(n => n.CreatedByUser))