EFCore The Linq Expression Could not be translated and will be evaluated locally

entity-framework entity-framework-core

Question

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);
    }
1
0
3/21/2018 9:30:34 PM

Accepted Answer

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);
1
3/21/2018 10:44:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow