I have a ticket entity that has ticketNotes (0 to many) and would like to pull back the user details for who created each note when I query tickets.
I use the following code to query a 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);
My TicketNote Class is:
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; }
}
I would like to add
.Include(t => t.Notes.CreatedByUser)
However as notes is a collection I don't get the option.
Please advise the best approach to achieve pulling back the CreatedByUser which is NULL at the moment.
Thanks
Any()would work here -.Include(t => t.Notes.Any().CreatedByUser)?