Requesting an Entity Structure to Include Child Entity Collection Objects

I have an object ticketthat has ticketNotes(from 0 to many) and would like to cancel user data for those who created each note when requesting tickets.

I use the following code to request 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 TicketNoteClass:

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'd like to add

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

However, since notes are a collection, I do not get this option.

Please advise the best approach to achieve the rejection of the Created User, which is currently NULL.

thank

+4
source share
2 answers

You can include more sophisticated applications for multilevel collections. Try the following:

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

ticket, Notes .

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

Source: https://habr.com/ru/post/1584211/


All Articles