繁体   English   中英

实体框架查询包括实体集合的子实体

[英]Entity framework query to include child entities of entity collection

我有一个具有ticketNotes (0到多个)的ticket实体,并希望在查询票证时撤回创建每个票据的用户详细信息。

我使用以下代码查询故障单

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))

你有ticket变量,里面有属性Notes 为了表现你可以使用

ticket.Notes.Select(t => n.CreatedByUser)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM