Skip to content

Not working with HasQueryFilter #81

Closed

Description

@igorcamasso

When we tried to use the projectable property with a global query filter on Entity<T>.HasQueryFilter(a=> a.projectableProperty), we got a The LINQ expression DbSet<T>().Where(n => n.projectableProperty == False) could not be translated.
Is this a bug or a some limitation?

Activity

  1. koenbeuk commented on Aug 18, 2023

    @koenbeuk
    Collaborator

    Just tried to reproduce this and it indeed looks like a compatibility bug. I don't know yet how to fix this as the library is either extending IQueryCompiler or IQueryTranslationPreprocessorFactory (depending on compatibility mode) however it seems that query filters are a completely different beast. Fun fact: I just learned that dbContext.Set<Entity>().ToQueryString() does not expand query filters either.

  2. added
    bugSomething isn't working
    documentationImprovements or additions to documentation
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    on Aug 18, 2023
  3. Seramis commented on Feb 21, 2025

    @Seramis

    Just stumbled on this limitation as well. :( We make heavy use of global filters (things like TenantId and UserId matched with data from session) and this library would be a perfect fit, if query filters would consider the projections, too.

    A simple, but very easily understandable use case would be that we have Tenant, which has List of User, which has List of Portfolio.

    I also have a system that enables queryfilters through interfaces. So what i want to do is to apply IHasTenant interface on Portfolio. That interface requires int TenantId { get; }. In Portfolio entity, i would like to write something like this:

    [Projectable] public int TenantId => User.Tenant.Id;
    
  4. added a commit that references this issue on Apr 9, 2025
    7e6fd8e
  5. added a commit that references this issue on May 4, 2025
    88c2db0
  6. added a commit that references this issue on May 4, 2025
    8afb193
  7. Seramis commented on Jun 19, 2025

    @Seramis

    I was able to solve this issue now based on the fix shown here, by calling the ExpandProjectables() on my own during the query filter registration.
    entityType.SetQueryFilter(queryFilter.ExpandProjectables() as LambdaExpression);

    Thank you!

    Any plans on the timing of next version of this library?
    The main branch based test builds seem to be not accessible to the "outsiders". Otherwise i would use currently the latest build from here: https://github.com/koenbeuk/EntityFrameworkCore.Projectables/pkgs/nuget/EntityFrameworkCore.Projectables

    EDIT:
    With the approach described above, I found that all the joins that would happen when projectable is evaluated, always happen even if i never select out any projectable properties. Seems like the ExpandProjectables() causes the joins happen no matter if the results are being used or not.
    I am not sure if this behavior also happens with the implementation done in PR #130.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions