pdevito3 / QueryKit

🎛️ QueryKit is a .NET library that makes it easier to query your data by providing a fluent and intuitive syntax for filtering and sorting.
Apache License 2.0
147 stars 13 forks source link

Add custom whole condition alias support #38

Closed AlexMKotcz closed 2 months ago

AlexMKotcz commented 4 months ago

Hello,

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

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

pdevito3 commented 4 months ago

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 commented 4 months ago

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

AlexMKotcz commented 4 months ago

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.

jveko commented 3 months ago

I really need this feature

pdevito3 commented 3 months ago

PRs are welcomed if you want to contribute

pdevito3 commented 2 months ago

resolved in 1.4 release with derived property api