I want to add conditional include based on given parameters from API. I wrote this down but seems not working and none of the includes are working
1 Answer
Awww, it was pretty simple,
IIncludableQueryable<ProductEntity, object?> dbSet = _context.Set<ProductEntity>().Include(i => i.Media);
if (dto.ShowCategories.IsTrue()) dbSet = dbSet.Include(i => i.Categories);
if (dto.ShowComments.IsTrue())
dbSet = dbSet.Include(i => i.Comments!.Where(x => x.ParentId == null))
.ThenInclude(x => x.Children)!
.ThenInclude(x => x.Media);
if (dto.ShowLocation.IsTrue()) dbSet = dbSet.Include(i => i.Locations);
if (dto.ShowForms.IsTrue()) dbSet = dbSet.Include(i => i.Forms);
if (dto.ShowMedia.IsTrue()) dbSet = dbSet.Include(i => i.Media);
if (dto.ShowReports.IsTrue()) dbSet = dbSet.Include(i => i.Reports);
if (dto.ShowTeams.IsTrue()) dbSet = dbSet.Include(i => i.Teams)!.ThenInclude(x => x.User).ThenInclude(x => x.Media);
if (dto.ShowVotes.IsTrue()) dbSet = dbSet.Include(i => i.Votes);
if (dto.ShowVoteFields.IsTrue()) dbSet = dbSet.Include(i => i.VoteFields);
if (dto.ShowCreator.IsTrue()) dbSet = dbSet.Include(i => i.User).ThenInclude(x => x!.Media);
