Using method parameter in linq query syntax in left join context results in an error

Open
#23,675 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, sql
Domain
database

Research direction

Start by reproducing GetDashboardInformationAsync with Microsoft.EntityFrameworkCore 5.0.1, comparing the parameterized date in the grouped left join with DateTime.Now. Investigate the reported GroupJoin translation failure; done means the query translates correctly with the method parameter, or the limitation and supported alternative are clearly documented.

Written by the indexing model from the issue text.

Description

area-query customer-reported

Hey,

I have a valid SQL-Statement which I would like to translate into LINQ query syntax:

SELECT TOP(1) columns-here...
FROM Employees AS emp
LEFT JOIN Absences AS abs ON emp.Id = abs.Id AND abs.Status = 1 AND  abs.From >= '2020-12-13'
WHERE emp.Id = 'GUID-here' AND (abs.Id IS NULL OR '2020-12-13' <= abs.From)
  public Task<DashboardInformation> GetDashboardInformationAsync(Guid employeeId, DateTime date)
  {
            return 
            (
                from employee in DbContext.Employees

                join absence in DbContext.Absences on employee .Id equals absence .EmployeeId into abs
                from absence in abs.Where(x => x.Status == Status.Accepted &&  x.From >= date).DefaultIfEmpty()
                 
                where employee .Id == employeeId && (absence == null || date<= absence .From)
                select new DashboardInformation
                {
                   // Mapping
                }).FirstOrDefaultAsync();
  }

I would like to be flexible with regard to the date, which is why I want to control it via method's parameter 'date'. But I get the following message:

fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
      Unhandled exception in circuit 'hckTJmGW1B9iLYQfDc5svMYL_VrJx4JBbK8WMxUiWYw'.
      System.InvalidOperationException: The LINQ expression 'DbSet<Employees>()
          .GroupJoin(
              inner: DbSet<Absences >(),
              outerKeySelector: employee => employee .Id,
              innerKeySelector: absence => absence .EmployeeId ,
              resultSelector: (employee , abs) => new {
                  employee = employee ,
                  abs= abs
               })' could not be translated. Either rewrite the query in a form that can be translated...

If I change

from absence in abs.Where(x => x.Status == Status.Accepted && x.From >= date).DefaultIfEmpty()

into

from absence in abs.Where(x => x.Status == Status.Accepted && x.From >= DateTime.Now).DefaultIfEmpty()

In other words, If I don't use the method's parameter everything works fine. But I need to control the date via a parameter.
Is that a bug or how can I achieve that?

I am using Microsoft.EntityFrameworkCore 5.0.1

Many thanks in advance!
-Lesterdor

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.