QueryKit icon indicating copy to clipboard operation
QueryKit copied to clipboard

Add custom whole condition alias support

Open AlexMKotcz opened this issue 1 year ago • 3 comments

Hello,

How about we get support for an alias for a custom whole condition?

Like, """AdultMale == true""" translating to """ Age >= 18 && Gender == "Male" """

AlexMKotcz avatar Apr 18 '24 02:04 AlexMKotcz

This seems like business logic creeping in a bit, which seems smelly on first glance, but I'm potentially open to it.

What api would you think for setting this up in the config?

pdevito3 avatar Apr 18 '24 03:04 pdevito3

I thought of another example today that i think this would be useful for -- more like aggregate properties like creating a FullName to search for an aggregate of first name and last name. need to think through the api a bit more and this would probably be tricky to get right under the hood, but glad to have some more examples for this

pdevito3 avatar Apr 29 '24 16:04 pdevito3

Hey there! I'm not sure about business logic creeping, you might be right. About the API, I thought we could pass an Expression<Func<T, bool>> and a string as a configuration, and then the library would know how to handle it.

var input = $"""AdultMale == true"""; //Only handles true or false
var config = new QueryKitConfiguration(config =>
{
    config.Filter<Person>(x => x.Age >= 18 && x.Gender == "Male")
     	 		.HasAliasName("AdultMale");
});

The reason I came up with this, is, I can have a readonly calculated property in an entity class that is ignored on the mapping to the DB (rather than having a Computed Column). In my example, the AdultMale is the property. In this case, trying to filter directly in the "AdultMale" property would cause an exception with EF Core, with a message like

The expression could not be translated

Thus the need for it to be translated to a query that the database can actually perform.

AlexMKotcz avatar May 03 '24 15:05 AlexMKotcz

I really need this feature

jveko avatar May 24 '24 04:05 jveko

PRs are welcomed if you want to contribute

pdevito3 avatar May 24 '24 04:05 pdevito3

resolved in 1.4 release with derived property api

pdevito3 avatar Jun 23 '24 00:06 pdevito3