Closed RubenDelange closed 1 year ago
@RubenDelange Thanks!
Do you mind pasting your LINQ here?
It's something like this:
.Where(ol => ol.Order.State == "SomeState")
.Select(ol => new OrderLineByDepartmentDto
{
DepartmentId = ol.Order.DepartmentId,
DepartmentName = ol.Order.Department.Name,
DepartmentCreatedDate = ol.Order.Department.CreatedDate,
MinTotalPrice = EF.Functions.Min(ol.TotalPrice, EF.Functions.Over().PartitionBy(ol.Order.DepartmentId)),
MinPrice = EF.Functions.Min(ol.Price, EF.Functions.Over().PartitionBy(ol.Order.DepartmentId)),
MinNumberOfItems = EF.Functions.Min(ol.NumberOfItems, EF.Functions.Over().PartitionBy(ol.Order.DepartmentId)),
})
.Distinct()
.OrderByDescending(olbd => olbd.DepartmentCreatedDate)
.ToListAsync()
In this example, only the MinTotalPrice = ...
is translated into SQL. If you swap around the MinXyz properties, only the first one is translated into SQL, the rest are ignored.
LINQ
var query = context.TestRows
.Select(r => new
{
Min = EF.Functions.Min(r.Id, EF.Functions.Over().PartitionBy(r.Id / 10)),
Max = EF.Functions.Max(r.Date, EF.Functions.Over().PartitionBy(r.Col1 / 10)),
Avg = EF.Functions.Avg(r.Id, EF.Functions.Over().PartitionBy(r.Col1 / 500)),
})
.Distinct();
Translates into
SELECT DISTINCT MIN([t].[Id]) OVER(PARTITION BY [t].[Id] / 10 ) AS [Min], MAX([t].[Date]) OVER(PARTITION BY [t].[Col1] / 10 ) AS [Max], AVG([t].[Id]) OVER(PARTITION BY [t].[Col1] / 500 ) AS [Avg]
FROM [TestRows] AS [t]
and fiddle
I suggest that if the problem persists you modify a fiddle or submit a PR with test case that clearly reproduces this issue.
Yeah, it seems like only 1 combination of [WindowFunction ; PartitionBy] is translated into SQL. So if you have multiple EF.Functions.Min(columnDoesntMatter, EF.Functions.Over().PartitionBy(partitionKey)
, only the first one is translated.
@RubenDelange thanks for reporting this.
Version 1.0.2 is on NuGet and this should be fixed. Please reopen this issue if that's not the case.
Lovely extension!
I do notice that it is not possible to do multiple MIN, MAX,... () OVER(PARTITION BY...) statements in a select query. Example: https://learn.microsoft.com/en-us/sql/t-sql/functions/max-transact-sql?view=sql-server-ver16#b-using-the-over-clause
Only the first one is actually translated into the correct SQL statement. The other ones are ignored.