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
You can include more complex statements to get at multilevel collections. Try the following:
.Include(t => t.Notes.Select(n => n.CreatedByUser))
You have ticket
variable which have property Notes
inside it. For performance you may use
ticket.Notes.Select(t => n.CreatedByUser)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.