I am attempting to create a compiled query that can be executed on the database side. The idea is to create a reusable function across my code. But EF Core is still saying this query cannot be translated to sql.
Here is the reusable
expression
public static class DisplayFilters
{
public static Expression<Func<DisplayItem, bool>> isActiveItem = (x) => x.IsDeleted == false;
}
Here is the EFCore entity call
using df = namespace.DisplayFilters;
...
[HttpGet]
public IActionResult Get()
{
IEnumerable<DisplayItem> xItem = (from a in _context.DisplayItem.AsQueryable()
where df.isActiveItem.Compile()(a)
select a);
return Ok(xItem);
}
I don't think you can embed Expression
type expressions in query comprehension syntax, but you can do it in lambda syntax:
IEnumerable<DisplayItem> xItem = _context.DisplayItem
.Where(df.isActiveItem);