I am trying to get flattened object from the database, but I get an error. I want to get 2 books for all genres in the database, the code looks like this:
IQueryable<Book> query = _context.Books
.GroupBy(b => b.Genre)
.SelectMany(bc => bc.Select(b => b).Take(2));
Does anyone know what am I doing wrong in here?
I get this exception instead of a result :
The LINQ expression '
bc => bc .AsQueryable() .Select(b => b) .Take(2)
' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See go.microsoft.com/fwlink/?linkid=2101038 for more information.
I tried also something like that :
IQueryable<Book> query = _context.Genres.GroupJoin(
_context.Books,
g => g,
b => b.Genre,
(g, books) => new
{
Genre = g,
BookCollection = books
}
).SelectMany(bc => bc.BookCollection.Select(b => b)
.Take(2)).Include(b => b.Author).Include(b => b.Rating)
.Include(b => b.BookISBNs).Include(b => b.Reviews)
.ThenInclude(r => r.User);