Search code examples
c#linqlinq-to-entities

Using an Expression with Linq


So I have the following linq query:

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 ...

and I have an Expression<Func<Org, bool>> variable that I want to use for the where clause. I can't figure out how to do that though with this style of linq query.

So ideally I'd do something like:

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 ...

Solution

  • Maybe build your query from Org:

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