Ad esempio, ho quelle entità:
public class Book
{
[Key]
public string BookId { get; set; }
public List<BookPage> Pages { get; set; }
public string Text { get; set; }
}
public class BookPage
{
[Key]
public string BookPageId { get; set; }
public PageTitle PageTitle { get; set; }
public int Number { get; set; }
}
public class PageTitle
{
[Key]
public string PageTitleId { get; set; }
public string Title { get; set; }
}
Come dovrei caricare tutti i PageTitles, se conosco solo il BookId?
Ecco come sto cercando di fare questo:
using (var dbContext = new BookContext())
{
var bookPages = dbContext
.Book
.Include(x => x.Pages)
.ThenInclude(x => x.Select(y => y.PageTitle))
.SingleOrDefault(x => x.BookId == "some example id")
.Pages
.Select(x => x.PageTitle)
.ToList();
}
Ma il problema è che getta un'eccezione
ArgumentException: l'espressione di proprietà 'x => {da Pages y in x select [y] .PageTitle}' non è valida. L'espressione dovrebbe rappresentare un accesso alla proprietà: 't => t.MyProperty'. Quando si specificano più proprietà, utilizzare un tipo anonimo: 't => new {t.MyProperty1, t.MyProperty2}'. Nome parametro: propertyAccessExpression
Cosa c'è che non va, cosa dovrei fare esattamente?
Prova ad accedere a PageTitle
direttamente in ThenInclude
:
using (var dbContext = new BookContext())
{
var bookPages = dbContext
.Book
.Include(x => x.Pages)
.ThenInclude(y => y.PageTitle)
.SingleOrDefault(x => x.BookId == "some example id")
.Select(x => x.Pages)
.Select(x => x.PageTitle)
.ToList();
}