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

Time:02-19

My private get all method

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

This is what i tried:

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

Error (Same error in .net 6 and 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.

CodePudding user response:

Well TakeLast can't be translated to SQL, but why you use TakeLast at all?

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

So order in the desired way and then use Take instead of TakeLast.

  • Related