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 Framework | NET Core

Documentation

QueryResultFilter

Properties
Name Description Example
ID Gets the QueryFilter ID. NET Framework / NET Core
EntityType Gets the QueryFilter entity type on which the filter is applied. NET Framework / NET Core
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 Framework / NET Core
Methods
Name Description Example
Enable() Enable the QueryFilter. NET Framework / NET Core
Disable() Disable the QueryFilter. NET Framework / NET Core

QueryResultFilterManager

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

Contents