Skip to content

System.Linq.Queryable.TakeLast() could not be translated. #25242

Closed as duplicate of#17065
@ghost

Description

Hello,

I have the following code:

Context.ApplicationVersions.OrderBy(x => x.ReleaseDate).TakeLast(versionsToRemoveCount).ToList();

The code throws an exception:

System.InvalidOperationException: 'The LINQ expression 'DbSet()
.OrderBy(x => x.ReleaseDate)
.TakeLast(__p_0)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

When I change the code to:

Context.ApplicationVersions.OrderByDescending(x => x.ReleaseDate).Take(versionsToRemoveCount).ToList();

everything works fine.

Data info:
ReleaseDate is type of DateTime, versionsToRemoveCount is type of Int32.

I am not sure if this behavior is correct or it is a bug.
Please take a look into it. Thank you!

EF Core version: 5.0.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.10.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions