Closed paritosharya408 closed 7 years ago
We should generate a case block because IS NULL
is condition and ordering should return a value.
Based on refactoring of BooleanConditionTranslatingExpressionVisitor
this should work in current dev.
Generated SQL
Executed DbCommand (21ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [x].[Id], [x].[ArticleText], [x].[BigImage], [x].[CustomUrl], [x].[Date], [x].[DetailedText], [x].[DocumentLocation], [x].[DocumentName], [x].[EventAddress], [x].[EventDate], [x].[EventRegistrationUrl], [x].[GroupNewsOrder], [x].[Header], [x].[HideOnListPage], [x].[IsCustomUrl], [x].[IsJobClosed], [x].[LinkType], [x].[LinkedGroupId], [x].[LinkedNewsId], [x].[NewsType], [x].[RegistrationClosed], [x].[SmallImage], [x].[SummaryText], [x].[UserDefinedRank]
FROM [News] AS [x]
ORDER BY CASE
WHEN [x].[UserDefinedRank] IS NULL
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END, [x].[UserDefinedRank], [x].[Date] DESC
fixed by #7554
I have following entity in my project (.net core1.0/ef core1.0/vs2015)-
Now I want to return all news items first ordered by NULL rank and then by any integer value and lastly also ordered by date descending. This is the code in my repository that returns this data
return await context.News.OrderBy(x => x.UserDefinedRank == null).ThenBy(x => x.UserDefinedRank).ThenByDescending(x => x.Date).ToListAsync();
This generates following SQL which is incorrect (viewed in SQL profiler):
The same code in EF6.0 works flawlessly and generates:
If I comment the OrderBy(x => x.UserDefinedRank == null), it returns data but without the first condition that i really want.
Steps to reproduce
Use OrderBy clause in LINQ with NULL condition.
Further technical details
EF Core version: 1.0.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio 2015