BaptX
BaptX

Reputation: 589

Manage boolean condition with Include in EF Core

I know is a simple problem, but I don't find the way to manage this properly. I think it's ugly, and I'm sure a better way exists.

    public async Task<List<DocumentCategory>> GetAll(bool includeDocumentTypes = false, bool includeDocumentDescriptions = false)
    {
        if (!includeDocumentTypes && !includeDocumentDescriptions)
        {
            return await _context.DocumentCategories.ToListAsync();
        }
        else if (includeDocumentTypes && !includeDocumentDescriptions)
        {
            return await _context.DocumentCategories.Include(dc => dc.DocumentTypes).ToListAsync();
        }
        else
        {
            return await _context.DocumentCategories.Include(dc => dc.DocumentTypes).ThenInclude(dt => dt.DocumentDescriptions).ToListAsync();
        }
    }

The goal is to include nested datas from boolean flags.

More information about my class below :

public class DocumentCategoriesRepository : GenericRepository<DocumentCategory>, IDocumentCategoriesRepository
{
    public DocumentCategoriesRepository(myDbContext context) : base(context)
    {

    }

    // Here is my method
}

Upvotes: 0

Views: 1877

Answers (3)

Nannanas
Nannanas

Reputation: 631

What about

public async Task<List<DocumentCategory>> GetAll(bool includeDocumentTypes = false, bool includeDocumentDescriptions = false)
    {
        var categories =  _context.DocumentCategories.AsQueryable();

        if (includeDocumentTypes)
        {
            categories = categories.Include(dc => dc.DocumentTypes);
        }

        if (includeDocumentDescriptions)
        {
            categories = categories
                              .Include(dc => dc.DocumentTypes)
                              .ThenInclude(dt => dt.DocumentDescriptions);
        }

        return await categories.ToListAsync();
    }

This should work as including DocumentTypes multiple times is not a problem for EF and is handled as if it was included just once.

Upvotes: 1

Mohamed Adel
Mohamed Adel

Reputation: 490

You can make simple edits like below

public async Task<List<DocumentCategory>> GetAll(bool includeDocumentTypes = false, bool includeDocumentDescriptions = false)
        {
            var lstCategoires = _context.DocumentCategories;

            if (includeDocumentTypes)
            {
                 lstCategoires = includeDocumentDescriptions ? lstCategoires.Include(dc => dc.DocumentTypes).ThenInclude(dt => dt.DocumentDescriptions) :
                                                               lstCategoires = lstCategoires.Include(dc => dc.DocumentTypes);
            }

            return await lstCategoires.ToListAsync();
        }

Upvotes: 0

phuzi
phuzi

Reputation: 13060

How about something like this, where you build up the query in distinct steps.

CAVEAT - This code hasn't been tested and may not work out of the box. The type of query would likely need some work

public async Task<List<DocumentCategory>> GetAll(bool includeDocumentTypes = false, bool includeDocumentDescriptions = false)
{
    var query = _context.DocumentCategories;

    if (includeDocumentTypes)
    {
        query = query.Include(dc => dc.DocumentTypes);

        if (includeDocumentDescriptions)
        {
            query = query.ThenInclude(dt => dt.DocumentDescriptions);
        }
    }

    return await query.ToListAsync()
}

Upvotes: 0

Related Questions