Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架查询,包括实体集合的子实体_C#_Entity Framework - Fatal编程技术网

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

C# 实体框架查询,包括实体集合的子实体,c#,entity-framework,C#,Entity Framework,我有一个ticket实体,它有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) sele

我有一个
ticket
实体,它有
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)

然而,由于notes是一个集合,我没有这个选项

请建议实现回调CreatedByUser的最佳方法,该CreatedByUser目前为空


谢谢

您有
票证
变量,其中包含属性
注释
。 对于性能,您可以使用

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

您可以包含更复杂的语句,以在多级集合中获取。请尝试以下操作:

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

不确定
Any()
是否可以在这里使用-
.Include(t=>t.Notes.Any().CreatedByUser)
?您好,谢谢,我刚刚尝试了这个方法,但是我没有任何实体选项,所以它不起作用。也许这篇文章可以帮助您:。特别是关于显式装载的部分。请您在退票时建议如何使用?我在我的存储库中返回一个票证实体,因此在上下文关闭后无法调用select。