首页 > 解决方案 > 我可以使用 .net core 3.1 中的连接表过滤 IQueryable 中的查询吗

问题描述

我有一个 IQeryable 作为具有多个输入的过滤器功能(为了清楚起见,我排除了一些):

    public IActionResult Index(... searchType, string searchUndertype ...)
    {
        var query = _context.ProjektDokument
          .Include(p => p.Dokumenttype)

          .Include(p => p.DokuUndertype)
          ...

          .AsQueryable();

        if (!string.IsNullOrEmpty(searchType))
        {
            query = query.Where(p => p.Dokumenttype.Dokumenttype1.Contains(searchType));
        }

        if (!string.IsNullOrEmpty(searchUndertype))
        {
            query = query.Where(p => p.DokuUndertype.DokuUndertype1.Contains(searchUndertype));
        }
        return View(query.ToList());
    }

这完美地工作。现在,用户想要添加一个功能,其中 Dokumenttype1 或 DokuUndertype1 的输入框中的结果被另一个过滤,因此只有已经创建的 DokumenttypeDokuUndertype 对在任一输入框中被选中,例如,如果您在dokumenttype1,您只会获得 A 在 dokumenttype1 中的所有实例,反之亦然。为此,我创建了一个连接表:

{
public partial class DokumenttypeDokuUndertype
{
    public int Id { get; set; }
    public int DokumenttypeId { get; set; }
    public int DokuUndertypeId { get; set; }

    public virtual DokuUndertype DokuUndertype { get; set; }
    public virtual Dokumenttype Dokumenttype { get; set; }
}

}

我的问题是,我该怎么做?我曾尝试通过布尔运算符将连接表包含在变量查询中,但无济于事。我已经尝试过.ThenInclude,并再次删除。我想也许视图模型可能是要走的路,但不太知道该怎么做。

你们有什么建议吗?(我使用了数据库优先方法)

标签: linqasp.net-coreentity-framework-core

解决方案


推荐阅读