在 Linq 中使用表达式

Using an Expression with Linq

所以我有以下 linq 查询:

var query = from s in context.vw_ActiveLabs
            join w in context.Worker on s.WWID equals w.WWID
            join o in context.Org on w.OrgKey equals s.OrgKey
            group ...

并且我有一个 Expression<Func<Org, bool>> 变量,我想将其用于 where 子句。尽管使用这种类型的 linq 查询,但我不知道如何做到这一点。

所以理想情况下我会做类似的事情:

Expression<Func<Org, bool>> orgFilter = ...;

var query = from s in context.vw_ActiveLabs
            join w in context.Worker on s.WWID equals w.WWID
            join o in context.Org on w.OrgKey equals s.OrgKey
            where orgFilter(o) == true
            group ...

也许可以从 Org 构建查询:

Expression<Func<Org, bool>> expression = @org => true;
Org.Where(expression.Compile())
      .Include(item => item.Worker)
      .ThenInclude(item => item.vw_ActiveLabs);