question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] ApplyFilter

See original GitHub issue

Hey Guys,

I hope I describe this clearly. I keep having issues with the PagiationQuery and in specific with the ApplyFilterWithoutPagination part.

I’ve made an Contract which has InventoryLines. The code below is the InventoryLinesPaginationQuery

using XVI.Application.Features.InventoryLines.Caching;
using XVI.Application.Features.InventoryLines.DTOs;

namespace XVI.Application.Features.InventoryLines.Queries.Pagination;

public class InventoryLinesWithPaginationQueryByContractId : PaginationFilterBase, ICacheableRequest<PaginatedData<InventoryLineDto>>
{
    [OperatorComparison(OperatorType.Equal)]
    public required int Id { get; set; }

    [CompareTo("ProductName")] // <-- This filter will be applied to Name or Description.
    [StringFilterOptions(StringFilterOption.Contains)]

    public string? Keyword { get; set; }

    public InventoryLineByContractIdListView ListView { get; set; } =
        InventoryLineByContractIdListView.All; //<-- When the user selects a different ListView,

    // a custom query expression is executed on the filter.
    public override string ToString()
    {
        return $"Listview:{ListView},Search:{Keyword},Sort:{Sort},SortBy:{SortBy},{Page},{PerPage}";
    }

    [IgnoreFilter] public string CacheKey => InventoryLineCacheKey.GetPaginationCacheKey($"{this}");
    [IgnoreFilter] public MemoryCacheEntryOptions? Options => InventoryLineCacheKey.MemoryCacheEntryOptions;
}

public class InventoryLinesWithPaginationByContractIdQueryHandler : IRequestHandler<
    InventoryLinesWithPaginationQueryByContractId, PaginatedData<InventoryLineDto>>
{
    private readonly IApplicationDbContext _context;
    private readonly IMapper _mapper;
    private readonly IStringLocalizer<InventoryLinesWithPaginationByContractIdQueryHandler> _localizer;

    public InventoryLinesWithPaginationByContractIdQueryHandler(
        IApplicationDbContext context,
        IMapper mapper,
        IStringLocalizer<InventoryLinesWithPaginationByContractIdQueryHandler> localizer
    )
    {
        _context = context;
        _mapper = mapper;
        _localizer = localizer;
    }

    public async Task<PaginatedData<InventoryLineDto>> Handle(InventoryLinesWithPaginationQueryByContractId request,
        CancellationToken cancellationToken)
    {

        var iQuerybleWithFilter= _context.InventoryLines.Where(x => x.ContractId == request.Id)
            .ApplyFilter(request);
        var resultsWithFilter = iQuerybleWithFilter.ToList(); // Yields 0 results 

        var iQuerableWithoutFilter= _context.InventoryLines.Where(x => x.ContractId == request.Id);
        var resultsWithoutFilter = iQuerableWithoutFilter.ToList(); // Yields 1 result         


        // TODO: Implement InventoryLinesWithPaginationQueryHandler method 
        var data = await _context.InventoryLines.Where(x => x.ContractId == request.Id)
            .ApplyFilterWithoutPagination(request)
            .ProjectTo<InventoryLineDto>(_mapper.ConfigurationProvider)
            .PaginatedDataAsync(request.Page, request.PerPage);
        return data;
    }
}

public class InventoryLinesByContractIdPaginationSpecification : Specification<InventoryLine>
{
    public InventoryLinesByContractIdPaginationSpecification(InventoryLinesWithPaginationQueryByContractId query)
    {
    }
}

public enum InventoryLineByContractIdListView
{
    [Description("All")] All,
}

There is factually 1 InventoryLine where the ContractId Matches the request.id. But why does ApplyFilter (or for that matter ApplyFilterWithoutPagination) take it away? I’m confused. As you can see in the code I cleaned all the filtering code (I had tons more listviews) but deleted them all

Edit: The question is in the handle method

Extra note @neozhu Thank you for your reply on my previous issue. Very warm and welcome.

This project is beautiful and once I learned more I hope I can write a README about the project itself cause I’ve some trouble finding my way trough the project. Its a beautiful setup and I love it tho. But lets say I don’t want caching on a page. Do I delete the code? should I put 0 minutes cache. Should I use a command or should I use a <Name>Query to export data. (Like the log page uses a command and the product page export pdf uses a query). Tho I love learning this project it feels it has everything I ever need for the setup of a cool application!

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
neozhucommented, Aug 4, 2023

Regarding the usage of the toString override, it is primarily used to generate a cache key. When the query conditions are the same, the generated key remains constant, which helps avoid redundant database queries.

If you want to implement more complex condition combinations, I suggest referring to the SearchProductSpecification.cs file. It provides an example of how to handle advanced search criteria.

0reactions
Bram1903commented, Aug 5, 2023

debugged with intellij

Sorry for my absence in replies, but I’m currently on vacation in Norway.

May I ask why you are debugging a .NET project (C#) in a Java based IDE? Of course I understand that you can install plugins to make that work, however I don’t understand why you would choose that over Rider.

Of course it could be that you don’t have all the IDE’s since those subscriptions are quite expensive. Luckily I can still enjoy the student benefits 😊

Nevertheless that still makes me wonder how you prefer an IDE which isn’t natively written for C# over Visual Studio. I find Visual Studio horrible btw, but I find it hard to believe Intelliji with a C# plugin works better than Visual Studio.

Kind regards, Bram

Read more comments on GitHub >

github_iconTop Results From Across the Web

DoCmd.ApplyFilter method (Access)
A string expression that is a valid SQL WHERE clause without the word WHERE. ControlName, Optional, Variant. Remarks. Use the ApplyFilter action ...
Read more >
ApplyFilter Macro Action
You can use the ApplyFilter action to apply a filter, a query, or an SQL WHERE clause to a table, form, or report...
Read more >
applyfilter question | Access World Forums
ApplyFilter , str End Sub. It asks me twice for a value. All I want to do is enter a dept and then...
Read more >
DoCmd.ApplyFilter Syntax
DoCmd.ApplyFilter Syntax ... Improve this question ... are concatenated by the database engine, and not by VBA, that shouldn't be a problem.
Read more >
Access Macro: ApplyFilter w/values from 2 combo boxes
What I'm trying to do is filter a column by the selections from two dropdowns on a form. I was able to do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found