0

I have the following code. It brings back the Show and its related Slides. However Slide also has related Items but I have no idea how to make the query include those (There is a foreign key in the db).

Show sh = (from s in _context.Shows
           where s.ShowId == id
           select new Show()
           {
               ShowId=s.ShowId,
               ShowName=s.ShowName,
               Slides=s.Slides           
           }).FirstOrDefault();

How do I modify this to make it also fetch the list of Items for each Slide?

I am using .Net5

3
  • Is there a reason you are not using LINQ for the whole query? Commented Apr 13, 2021 at 9:02
  • Yes, I do not like it. Linq Query Syntax is entirely valid code and in many cases looks nicer in my mind. This Query could get quite complex. It just looks simple right now. Commented Apr 13, 2021 at 9:13
  • @CompiledIO What is not LINQ in this query? Query syntax is also LINQ, if that is what you mean. Commented Apr 13, 2021 at 9:32

1 Answer 1

1

If you defined your DbContext correctly the LINQ would be

Show sh = _context.Shows
    .Where(s => s.ShowId == id)
    .Include(s => s.Slides)
    .ThenInclude(sl => sl.Items)
    .FirstOrDefault();
Sign up to request clarification or add additional context in comments.

1 Comment

This works thanks. Just to complete the Answer. In Query Syntax the code is Show sh = (from s in _context.Shows where s.ShowId == id select s).Include(x=>x.Slides).ThenInclude(x=>x.Panes).FirstOrDefault();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.