Entity Framework Classic Query Result Filter

Description

The QueryResultFilter feature allows you to filter the result of a query. The filter is not performed on a database but in the result returned from the query.

It's always recommended to use the QueryFilter over the QueryResultFilter whenever you can to reduce the number of rows returned from the Database. However, the QueryResultFilter might also be useful if your filter cannot be translated into an expression.

Example

public class EntityContext : DbContext
{
    public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
    {
        // Add your QueryResultFilter here
        this.Configuration.QueryResultFilter.Filter<ISoftDelete>(customer => !customer.IsDeleted || DisplaySoftDelete());
    }
    
    public bool DisplaySoftDelete()
    {
        // Check for example if current user has admin right
        return false;
    }
    
    public DbSet<Customer> Customers { get; set; }
}

var list = context.Customers.ToList();

Try it: NET Core | NET Framework

Documentation

QueryResultFilter

Properties
Name Description Example
ID Gets the QueryFilter ID. NET Core / NET Framework
EntityType Gets the QueryFilter entity type on which the filter is applied. NET Core / NET Framework
IsEnabled Gets if the QueryFilter is enabled. Use Enable() and Disable() method to change the state. Always return false if the QueryFilter feature is disabled. NET Core / NET Framework
Methods
Name Description Example
Enable() Enable the QueryFilter. NET Core / NET Framework
Disable() Disable the QueryFilter. NET Core / NET Framework

QueryResultFilterManager

Properties
Name Description Example
IsEnabled Gets or sets if the QueryFilter feature is enabled. NET Core / NET Framework
Methods
Name Description Example
Filter<T>(Expression<Func<T, bool>> filter) Filter an entity type using a predicate. NET Core / NET Framework
Filter<T>(string id, Expression<Func<T, bool>> filter) Filter an entity type using a predicate. The QueryFilter will be created with the specified ID. NET Core / NET Framework
EnableFilter(string id) Enable the QueryFilter with the specified id. NET Core / NET Framework
DisableFilter(string id) Disable the QueryFilter with the specified id. NET Core / NET Framework
GetFilter(string id) Get the QueryFilter with the specified id. NET Core / NET Framework

Contents