Entity Framework Filter “Expression”

Jon and Tim already explained why it doesn’t work.

Assuming that the filter code inside Filter is not trivial, you could change Filter so that it returns an expression EF can translate.

Let’s assume you have this code:

context.Table.Where(x => x.Name.Length > 500);

You can now create a method the returns this expression:

Expression<Func<YourEntity, bool>> FilterByNameLength(int length)
{
    return x => x.Name.Length > length;
}

Usage would be like this:

context.Table.Where(FilterByNameLength(500));

The expression you build inside FilterByNameLength can be arbitrarily complex as long as you could pass it directly to Where.

Leave a Comment