I have a Linq2Sql Query like this:
Parent.Include(p => p.Children)
.Where(p => p.Children.Any(c => c.SomeNullableDateTime == null)
&& p.Children
.Where(c => c.SomeNullableDateTime == null)
.OrderBy(c => c.SomeInteger)
.First()
.SomeOtherNullableDateTime != null
)
.Select(p => p.Children
.Where(c => c.SomeNullableDateTime == null)
.OrderBy(c => c.SomeInteger)
.First()
.SomeOtherNullableDateTime)
.ToList();
This worked fine until moving from EF core 5 to EF core 6. With EF core 6 the result list contains some null values (which should not be the case because the where condition asks for not null). Is there some breaking change / limitation in EF core 6 I'm not aware of or is this simply a bug?
Update: This is an excerpt of the output
Update 2: This is the generated SQL Statement
SELECT(
SELECT TOP(1)[p1].[SomeOtherNullableDateTime]
FROM[Children] AS[p1]
WHERE([p].[Id] = [p1].[ParentId]) AND[p1].[SomeNullableDateTime] IS NULL
ORDER BY[p1].[SomeInteger])
FROM[Parent] AS[p]
WHERE EXISTS(
SELECT 1
FROM[Children] AS [c]
WHERE ([p].[Id] = [c].[ParentId]) AND[c].[SomeNullableDateTime] IS NULL) AND EXISTS(
SELECT 1
FROM[Children] AS [c0]
WHERE ([p].[Id] = [c0].[ParentId]) AND[c0].[SomeNullableDateTime] IS NULL)
GO
So it looks like the problem is that SomeOtherNullableDateTime (which is supposed to be not null) is not even included in the where clause of the generated SQL.
Update 3: This is the SQL EF core 5 (correctly) generates
SELECT (
SELECT TOP(1) [c].[SomeOtherNullableDateTime]
FROM [Children] AS [c]
WHERE ([p].[Id] = [c].[ParentId]) AND [c].[SomeNullableDateTime] IS NULL
ORDER BY [c].[SomeInteger])
FROM [Parent] AS [p]
WHERE EXISTS (
SELECT 1
FROM [Children] AS [c0]
WHERE ([p].[Id] = [c0].[ParentId]) AND [c0].[SomeNullableDateTime] IS NULL) AND (
SELECT TOP(1) [c1].[SomeOtherNullableDateTime]
FROM [Children] AS [c1]
WHERE ([p].[Id] = [c1].[ParentId]) AND [c1].[SomeNullableDateTime] IS NULL
ORDER BY [c1].[SomeInteger]) IS NOT NULL
GO
