EntityFramework.DynamicFilters icon indicating copy to clipboard operation
EntityFramework.DynamicFilters copied to clipboard

Apply/disable filter per query

Open hikalkan opened this issue 10 years ago • 3 comments

Hi,

I want to use SoftDelete pattern. But, sometimes we may need to deleted entities. It should work per query. Example:

DbContext.Set<User>().AsQueryable().DisableFilter("SoftDeleteFilter").Where(u => u.DeletionTime > SOME_TIME).ToList();

Is this possible? This is my project: https://github.com/aspnetboilerplate/aspnetboilerplate

hikalkan avatar Mar 04 '15 17:03 hikalkan

This is something I was thinking about recently too. And also to be able to set parameter values on a per query basis. But, I'm not sure there is a way that I can store that information attached to the query and then retrieve it again during the query parameter substitution. But I'll give it some more thought and see if I can some up with something.

In the meantime, you can disable the filter (which is scoped to just that DbContext instance) before executing the query and then re-enable it after the query. It's not as clean as the syntax you're proposing, but it will get the job done.

jcachat avatar Mar 05 '15 04:03 jcachat

Thanks for your answer. I want to hear if you can find a good solution.

hikalkan avatar Mar 05 '15 15:03 hikalkan

random thought @jachat how about an extension method for an IQueryableT that takes the context in so you can do that behind the scenes, the resulting code to give you literally everything you needed might look something like ...

DbContext.Set<User>()
    .AsQueryable()
    .DisableFilter("SoftDeleteFilter", DbContext)
    .Where(u => u.DeletionTime > SOME_TIME)
    .ToList()
    .ReEnableFilters(DbContext);

its not perfect but it does allow the functionality and abstracts from the current querying code the extra calls to remove or add the filter as needed.

You may not need the ReEnable bit on the end if you "track" the next call actually executed on the context from when the disable filter extension is called somehow (but I admit I don't know enough about what you are seeing in terms of variables to hand in such a context)

TehWardy avatar Mar 30 '17 13:03 TehWardy