EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

Filter By Generically By Type

Open granthoff1107 opened this issue 7 years ago • 6 comments

This is a small feature request but I think it would be useful to the project, The ability to filter by type not Generically

Current Behavior:

this.Filter<Conversation>(c => c.Where(x => x.EmployeeUserId == userId || x.EmployerUserId == userId));

I would like to be able to filter like so:

Func<IQuerable<Conversation>, IQueryable<Conversation> conversationFilter = c => c.Where(x => x.EmployeeUserId == userId || x.EmployerUserId == userId);
this.Filter(typeof(Conversation), conversationFilter )

granthoff1107 avatar Jan 05 '18 05:01 granthoff1107

Hello @granthoff1107 ,

Sorry for the delay, it seems we forget to log this request.

We will try to look at it this week.

Best Regards,

Jonathan

JonathanMagnan avatar Jan 28 '18 15:01 JonathanMagnan

@JonathanMagnan Any updates on this?

granthoff1107 avatar Mar 25 '18 22:03 granthoff1107

Hello @granthoff1107 ,

My developed look at it today and he is not sure to understand as it should already work with this code:

Func<IQueryable<EntitySimple>, IQueryable<EntitySimple>> conversationFilter = c => c.Where(x => x.ID == 1);
this.Filter(conversationFilter);

You do not have to specify the type since the generic type gets auto-resolved using the parameter specified.

Best Regards,

Jonathan

JonathanMagnan avatar Apr 01 '18 14:04 JonathanMagnan

I’m aware of that. But this is a different use case. There is a possibility you want to register generically using an injected configuration.

For instance say you pass on a

Dictionary<Type, LambdaExpression>

And you want to use that to build your filters. Currently the only way to do so would be through heavy reflection.

granthoff1107 avatar Apr 02 '18 02:04 granthoff1107

Sorry to be more clear the code would look more like the following

Dictionary<Type, LambdaExpression> filterMap;
foreach(var filter in filterMap)
{
    var filterExpression = context.Set(filter.Key).Where(filter.Value);
    Filter(filter.Key, filterExpression)
}

This will generically filter from a map. Sorry this is pseudo code I’m doing on my phone but it should make sense

granthoff1107 avatar Apr 02 '18 02:04 granthoff1107

Hello @granthoff1107 ,

Yes, that's more clear.

You want to add a filter using an Expression or LambdaExpression. On our side, we will do the reflection and add the generic method correctly.

I will give it to one of my developers next week.

Best Regards,

Jonathan

JonathanMagnan avatar Apr 05 '18 13:04 JonathanMagnan