Hello,
The following is the SQL query i am trying to produce in LINQ:
SELECT *
FROM `CustomerMainTable` AS `t`
WHERE `t`.`CustomerMainTableId` IN (
SELECT MAX(`t0`.`CustomerMainTableId`)
FROM `CustomerMainTable` AS `t0`
GROUP BY `t0`.`CustomerMainTableCustomerId`
)
CustomerMainTableId is an autoincrement id, and CustomerMainTableCustomerId is the customer's unique ID. There can be multiple entries of the customer's transaction in the CustomerMainTableId, so the idea is to select the latest transaction for each customer by grouping and selecting the max id.
In entity framework core 3.1, this was achieved via:
var CustomerGroupQuery = _DB.CustomerMainTable.Where(p => _DB.CustomerMainTable
.GroupBy(l => l.CustomerMainTableCustomerId)
.Select(g => g.Max(c => c.CustomerMainTableId))
.Contains(p.CustomerMainTableId));
However, the same query, when executed in EF Core 6 or 7 produces a wrong query in SQL:
SELECT *
FROM `CustomerMainTable` AS `t`
WHERE EXISTS (
SELECT 1
FROM `CustomerMainTable` AS `t0`
GROUP BY `t0`.`CustomerMainTableCustomerId`
HAVING MAX(`t0`.`CustomerMainTableId`) = `t`.`CustomerMainTableId`)
Interestingly enough, if we split the Linq query like so:
List<int> idlist = _DB.CustomerMainTable
.GroupBy(l => l.CustomerMainTableCustomerId)
.Select(g => g.Max(c => c.CustomerMainTableId)).ToList();
And then feed the list of integers back to the contains query:
var CustomerGroupQuery = _DB.CustomerMainTable.Where(p => idlist.Contains(p.CustomerMainTableId));
It produces the correct query. However, feeding the list of integers back to the contains query means this is a client side evaluation which is not ideal.
Hello,
The following is the SQL query i am trying to produce in LINQ:
CustomerMainTableId is an autoincrement id, and CustomerMainTableCustomerId is the customer's unique ID. There can be multiple entries of the customer's transaction in the CustomerMainTableId, so the idea is to select the latest transaction for each customer by grouping and selecting the max id.
In entity framework core 3.1, this was achieved via:
However, the same query, when executed in EF Core 6 or 7 produces a wrong query in SQL:
Interestingly enough, if we split the Linq query like so:
And then feed the list of integers back to the contains query:
It produces the correct query. However, feeding the list of integers back to the contains query means this is a client side evaluation which is not ideal.