Incorrect query being produced
See original GitHub issueThe issue
Using the below code, the query is not being produced correctly. Using other EF providers, it produces the correct expected query.
// In this example; order=date; sort=desc
var qEvents = _db.ServerEvents
.Where(e => e.Server.FarmId == farmId && e.ServerId == serverId)
.Skip(offset)
.Take(limit);
switch (order)
{
case "id":
if (sort == "asc")
qEvents = qEvents.OrderBy(e => e.Id);
else
qEvents = qEvents.OrderByDescending(e => e.Id);
break;
case "user":
if (sort == "asc")
qEvents = qEvents.OrderBy(e => e.User.Email);
else
qEvents = qEvents.OrderByDescending(e => e.User.Email);
break;
case "date":
if (sort == "asc")
qEvents = qEvents.OrderBy(e => e.TimeStamp);
else
qEvents = qEvents.OrderByDescending(e => e.TimeStamp);
break;
}
var events = await qEvents.ToArrayAsync(cancellationToken);
Produced Query
SELECT `t`.`id`, `t`.`EventParameter`, `t`.`EventType`, `t`.`ServerId`, `t`.`Reason`, `t`.`Status`, `t`.`TimeStamp`, `t`.`UserId`
FROM (
SELECT `s`.`id`, `s`.`EventParameter`, `s`.`EventType`, `s`.`ServerId`, `s`.`Reason`, `s`.`Status`, `s`.`TimeStamp`, `s`.`UserId`
FROM `server_events` AS `s`
INNER JOIN `servers` AS `s0` ON `s`.`ServerId` = `s0`.`id`
WHERE (`s0`.`FarmId` = @__farmId_0) AND (`s`.`ServerId` = @__serverId_1)
LIMIT @__p_3 OFFSET @__p_2
) AS `t`
ORDER BY `t`.`TimeStamp` DESC
Expected Query
SELECT `s`.`id`, `s`.`EventParameter`, `s`.`EventType`, `s`.`ServerId`, `s`.`Reason`, `s`.`Status`, `s`.`TimeStamp`, `s`.`UserId`
FROM `server_events` AS `s`
INNER JOIN `servers` AS `s0` ON `s`.`ServerId` = `s0`.`id`
WHERE (`s0`.`FarmId` = @__farmId_0) AND (`s`.`ServerId` = @__serverId_1)
ORDER BY `s`.`TimeStamp` DESC
LIMIT @__p_2 OFFSET @__p_2
Further technical details
MySQL version: 8.0.23 Operating system: CentOS 8 Pomelo.EntityFrameworkCore.MySql version: 6.0.1
Issue Analytics
- State:
- Created a year ago
- Comments:8
Top Results From Across the Web
Query producing incorrect results - python
Query producing incorrect results ... I realize that there is a chance it will try to sort the same table twice, but the...
Read more >Incorrect query result - SQL Server 2016
I'm seeing incorrect result returned by a query I just ran this morning in production (I ran the same query last night couple...
Read more >Top 5 Reasons for Wrong Results in SQL Server
Many people have come across situations where their queries are returning incorrect results - sometimes this is due to a bug in SQL...
Read more >Incorrect query results for execution plan, unsure of exact ...
I'm not familiar with a way to view execution plan in production and running the query in sql studio didn't result in same...
Read more >SQL query and folding produces incorrect results
Solved: I have a query that like this: let //Variables - Business Unit: BusinessUnitFilterList = Text.Split(#"Business Unit Filter",
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
EF and EF Core are two completely different products, that do similar things, but don’t share a codebase. So differences in their translations are not unexpected.
@johnwc This appears to be incorrect in my tests. I used the following console project:
Program.cs
All tested providers produce the same nested query (using EF Core 6):
Pomelo (MySQL)
SQL Server
Npgsql (PostgreSQL)
Sqlite
The SQL statements and clauses should reflect your LINQ query as much as possible, so that you get an expected result.
In your case, since you first call
Skip()
, thenTake()
and finallyOrderByDescending()
, the only way to accurately reflect this is to create a sub query, which EF Core does for all providers.It also outputs a warning, as you can see in all console outputs above:
So: order first, then skip and take.