Using method parameter in linq query syntax in left join context results in an error
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100