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

Help using count operator #40

Closed AlexMKotcz closed 4 months ago

AlexMKotcz commented 4 months ago

Hello, I'm sorry, but can you please explain how to use the #== and #!= operators? Just one example is enough. Thanks in advance.

pdevito3 commented 4 months ago

You can use it to filter on the count of a collection, you can prefix some of the operators with a #. For example, if i wanted to get all recipes that have more than 0 ingredients:

var input = """"Ingredients #>= 0"""";

full test example:

    [Fact]
    public async Task can_filter_by_string_for_collection_with_count()
    {
        // Arrange
        var testingServiceScope = new TestingServiceScope();
        var faker = new Faker();
        var fakeIngredientOne = new FakeIngredientBuilder()
            .WithName(faker.Lorem.Sentence())
            .Build();
        var fakeRecipeOne = new FakeRecipeBuilder().Build();
        fakeRecipeOne.AddIngredient(fakeIngredientOne);

        var fakeIngredientTwo = new FakeIngredientBuilder()
            .WithName(faker.Lorem.Sentence())
            .Build();
        var fakeRecipeTwo = new FakeRecipeBuilder().Build();
        fakeRecipeTwo.AddIngredient(fakeIngredientTwo);
        await testingServiceScope.InsertAsync(fakeRecipeOne, fakeRecipeTwo);

        var input = $"""Title == "{fakeRecipeOne.Title}" && Ingredients #>= 1""";

        // Act
        var queryableRecipes = testingServiceScope.DbContext().Recipes;
        var appliedQueryable = queryableRecipes.ApplyQueryKitFilter(input);
        var recipes = await appliedQueryable.ToListAsync();

        // Assert
        recipes.Count.Should().Be(1);
        recipes[0].Id.Should().Be(fakeRecipeOne.Id);
    }
AlexMKotcz commented 4 months ago

I understand, but my question is specifically about the operators #== and #!=, I couldn't find a use case.

pdevito3 commented 4 months ago

You mean general use cases? In the example above you could filter for recipes that only have 3 ingredients

Or are you saying the above doesn't actually work and it should?

AlexMKotcz commented 4 months ago

Hmm I mean, I'm asking about the #== and #!= operators but your answer only mentions the #>=, I want to know about the Equals and Not Equals Count Operator.

pdevito3 commented 4 months ago

Is that not what I just called out? The actual usage would look like var input = """"Ingredients #== 3""""; same as the others just with the given operator

AlexMKotcz commented 4 months ago

Oops! Now I get it. Thanks a lot.