using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; namespace DataLayer { public static class BookQueries { public static int BooksWithoutDetailsCount() { using var context = LibationContext.Create(); return context .Books .Count(b => !b.HasBookDetails); } public static Book GetBook_Flat_NoTracking(string productId) { using var context = LibationContext.Create(); return context .Books .AsNoTracking() .GetBook(productId); } public static Book GetBook(this IQueryable books, string productId) => books .GetBooks() .SingleOrDefault(b => b.AudibleProductId == productId); /// This is still IQueryable. YOU MUST CALL ToList() YOURSELF public static IQueryable GetBooks(this IQueryable books, Expression> predicate) => books .GetBooks() .Where(predicate); public static IQueryable GetBooks(this IQueryable books) => books // owned items are always loaded. eg: book.UserDefinedItem, book.Supplements .Include(b => b.SeriesLink).ThenInclude(sb => sb.Series) .Include(b => b.ContributorsLink).ThenInclude(c => c.Contributor) .Include(b => b.Category).ThenInclude(c => c.ParentCategory); } }