使用包含在 EF Core 中管理布尔条件

时间:2021-02-25 09:58:05

标签: c# .net-core entity-framework-core

我知道这是一个简单的问题,但我没有找到正确管理它的方法。我认为这很丑陋,而且我确信存在更好的方法。

var certificates = _db.Certificates
                      .Join(_db.Businesses,
                            certificate => certificate.BusinessId,
                            business => business.Id,
                            (certificate, business) => new { certificate, business })
                      .Join(_db.BusinessUsers.Where(user => user.Email = _user.Name),
                            certificateAndBusiness => certificateAndBusiness.business.Id,
                            businessUser => businessUser.BusinessId,
                            (certificateAndBusiness, businessUser) => certificate)
                      .GroupBy(certificate => certificate.Id)
                      .Select(certificates => certificates.First())
                      .Select(certificate => new CertificateListItemModel() {...})
                      .ToList()
```

目标是包含来自布尔标志的嵌套数据。

关于我的课程的更多信息如下:

    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();
        }
    }

3 个答案:

答案 0 :(得分:1)

怎么样

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();
    }

这应该是有效的,因为多次包含 DocumentTypes 对 EF 来说不是问题,并且就像只包含一次一样进行处理。

答案 1 :(得分:0)

像这样的事情如何,您可以在不同的步骤中构建查询。

CAVEAT - 此代码尚未经过测试,可能无法立即使用。 query 的类型可能需要一些工作

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()
}

答案 2 :(得分:0)

您可以进行如下简单的编辑

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();
        }