C# IQueryable - TakeLast(__p_0)' 无法翻译错误

C# IQueryable - TakeLast(__p_0)' could not be translated Error

我的私有get all方法

    private IQueryable<Category> getAll()
        => _unitOfWork.CategoryRepo
               .GetAll()
               .Include(x => x.CategoryDetails)
               .Include(x => x.Categories)
               .ThenInclude(x => x.CategoryDetails);

这是我尝试过的:

await getAll().OrderBy(x => x.Id).TakeLast(1000).ToListAsync()

错误(.net 6 和 3.1 中的相同错误):

The LINQ expression 'DbSet<Category>()
    .Include(x => x.CategoryDetails)
    .Include(x => x.Categories)
    .ThenInclude(x => x.CategoryDetails)
    .OrderBy(x => x.Id)
    .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'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

嗯,TakeLast 不能翻译成 SQL,但你为什么要用 TakeLast

await getAll().OrderByDescending(x => x.Id).Take(1000).ToListAsync()

所以按照需要的方式排序,然后使用 Take 而不是 TakeLast