1

My domain model is:

Book

public class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ReleaseYear { get; set; }

    public virtual ICollection<Store> Stores { get; set; }
}

Store

public class Store
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int BookId { get; set; }

    public virtual Book Book { get; set; }
    public virtual ICollection<StoreLocation> Locations { get; set; }
}

StoreLocation

public class StoreLocation
{
    public int Id { get; set; }
    public string Address { get; set; }
    public int StoreId { get; set; }

    public virtual Store Store { get; set; }
}

How can include all the levels (and sublevels) of a Book?

public Book GetBookById(int Id)
{
    return this._DbContext.Books
        .Include(p => p.Stores)
        .FirstOrDefault(p => p.Id == Id);
}

What i tried?

  • .ThenInclude(p => p.StoreLocation) doesn't work.
2
  • 1
    Did you try ThenInclude? Commented Nov 1, 2016 at 12:16
  • Yes. When is a List in another List i cannot include the second one. Commented Nov 1, 2016 at 12:22

1 Answer 1

1

You have to do it as shown below.

  return this._DbContext.Books.Include(p => p.Stores).ThenInclude(q => q.Locations)
                    .FirstOrDefault(p => p.Id == Id);

Note : Sometimes VS doesn't show intelisence properly.So beware of intellisense :D .One solution may be for that is,close the VS and restart new instance of it.If it doesn't work, just type what you want without relying on the intelisence. After that it compiles and works properly.

Sign up to request clarification or add additional context in comments.

7 Comments

It doesn't work. On the .ThenInclude(q => q... it shows me the methos for a list, i suppose the first one.
according to your models, it should be a list no ? then what is the problem there ?
No, it doesn't work. Yes is a List containing another List. I cannot include the second one (Locations). When use .ThenInclude i don't have the Location poperty. Instead i have the generic methods for a list.
what did you mean by generic method ? this is your list property no ? public virtual ICollection<StoreLocation> Locations { get; set; }.So why can't you use that then ? with your models where I can use like that.why you can't ?
That is due to intelisence issue on the VS.without considering that just type the code which I have shown above and compile it.All will be ok then.Later you can restart the new instance of VS.
|

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.