Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query with All and Any evaluates locally #15683

Closed
mdmoura opened this issue May 10, 2019 · 3 comments
Closed

Query with All and Any evaluates locally #15683

mdmoura opened this issue May 10, 2019 · 3 comments

Comments

@mdmoura
Copy link

mdmoura commented May 10, 2019

Using Entity Framework Core 2.2 I am trying to execute the following query on the database:

var user = context.Users.AsNoTracking()
  .Include(x => x.UserSkills).ThenInclude(x => x.Skill)
  .Include(x => x.UserSkills).ThenInclude(x => x.SkillLevel)
  .FirstOrDefault(x => x.Id == userId);

Then I tried the following query:

var lessons = _context.Lessons.AsNoTracking()
  .Where(x => x.LessonSkills.All(y => 
     user.UserSkills.Any(z => y.SkillId == z.SkillId && y.SkillLevelId <= z.SkillLevelId)))
  .ToList();

Exception

This lessons query evaluates in memory and I get the following exception:

The LINQ expression 'where (([y].SkillId == [z].SkillId) AndAlso ([y].SkillLevelId <= [z].SkillLevelId))' could not be translated and will be evaluated locally.'.

I tried a few options like creating a userSkills variable:

var userSkills = user.UserSkills.Select(z => new { 
  SkillId = z.SkillId, 
  SkillLevelId = z.SkillLevelId 
});

And then use userSkills in lessons query instead of user.UserSkills.

But still evaluates locally ... Is this a bug?

Further technical details

EF Core version: 2.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: macOS Mojave
IDE: Visual Studio Code 1.33.1

@sdanyliv
Copy link

It do not looks like a bug but misunderstanding how IQueryable works.
Rewrite in the following way (source for method chain should be IQueryable but not just list or collection):

var userSkills = _context.UserSkills
   .Where(us => us.UserId == userId)
   .Select(z => new { 
       SkillId = z.SkillId, 
       SkillLevelId = z.SkillLevelId 
    });

var lessons = _context.Lessons.AsNoTracking()
  .Where(x => x.LessonSkills.All(y => 
     userSkills.Any(z => y.SkillId == z.SkillId && y.SkillLevelId <= z.SkillLevelId)))
  .ToList();

@ajcvickers ajcvickers added this to the Backlog milestone May 17, 2019
@smitpatel smitpatel removed their assignment Jun 24, 2019
@ajcvickers ajcvickers added verify-fixed This issue is likely fixed in new query pipeline. and removed consider-for-current-release labels Sep 4, 2019
@ajcvickers ajcvickers modified the milestones: Backlog, 3.1.0 Sep 4, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0, Backlog Oct 11, 2019
@ajcvickers ajcvickers modified the milestones: Backlog, 5.0.0 Nov 13, 2019
@smitpatel
Copy link
Contributor

Exception in 3.1

Unhandled exception. System.InvalidOperationException: The LINQ expression 'DbSet<LessonSkill>
    .Where(l0 => EF.Property<Nullable<int>>((EntityShaperExpression:
        EntityType: Lesson
        ValueBufferExpression:
            (ProjectionBindingExpression: EmptyProjectionMember)
        IsNullable: False
    ), "Id") != null && EF.Property<Nullable<int>>((EntityShaperExpression:
        EntityType: Lesson
        ValueBufferExpression:
            (ProjectionBindingExpression: EmptyProjectionMember)
        IsNullable: False
    ), "Id") == EF.Property<Nullable<int>>(l0, "LessonId"))
    .All(l0 => __user_UserSkills_0
        .Any(z => l0.SkillId == z.SkillId && l0.SkillLevelId <= z.SkillLevelId))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

@smitpatel
Copy link
Contributor

Duplicate of #19070

@smitpatel smitpatel marked this as a duplicate of #19070 Dec 10, 2019
@smitpatel smitpatel removed this from the 5.0.0 milestone Dec 10, 2019
@smitpatel smitpatel added closed-duplicate and removed punted-for-3.1 type-bug verify-fixed This issue is likely fixed in new query pipeline. labels Dec 10, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants