Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading