entity-framework-coreasp.net-core-webapiodata.net-6.0

OData + EF Core + incorrect orderby executed with join query


In C# with an ASP.NET Core Web API, I have an OData endpoint against which I am executing this query:

var baseQuery = from flight in _context.TblAfdFlights
                join airline in _context.TblAirlines
                    on flight.FAirline equals airline.AlCode
                where !flight.IsDeleted.HasValue
                select new
                {
                    flight,
                    Logo = airline.AL_Logo_New
                };

When I use an OData query like:

{url}/api/Flights?$orderby=fNr%20asc&$top=10&$skip=0

EF Core is executing orderby on multiple properties:

SELECT [t].[F_ID], ...all flight props..., [t0].[AL_Logo_New] AS [Logo]
FROM [TBL_AFD_Flight] AS [t]
INNER JOIN [Tbl_Airline] AS [t0] ON [t].[F_Airline] = [t0].[AL_Code]
WHERE [t].[IsDeleted] IS NULL
ORDER BY [t].[F_Nr], [t0].[AL_Logo_New]
OFFSET @__TypedProperty_0 ROWS FETCH NEXT @__TypedProperty_1 ROWS ONLY

How to correct the behavior such that order by is applied only fNr as stated in the query?


Solution

  • Answer was to apply below attribute:
    [EnableQuery(EnsureStableOrdering = false)]

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnet.odata.enablequeryattribute.ensurestableordering?view=odata-aspnetcore-7.0