Click here to Skip to main content
15,918,268 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the following code to get paged data to the DataGerid:

using (var _sqliteContext = new SQLiteContext())
                {
                    AssociatedObject.LoadDynamicItems(e.StartIndex, await _sqliteContext.Equipments
                        .Include(x => x.CostCenter)
                        .Include(x => x.EqInfo)
                        .Skip(e.StartIndex)
                        .Take(e.PageSize)
                        .ToListAsync());
                }


I want to reverse the order of the whole data meaning the last data added is shown on the first page by default. How can I do it?

What I have tried:

I tried this:
using (var _sqliteContext = new SQLiteContext())
                {
                    AssociatedObject.LoadDynamicItems(e.StartIndex, await _sqliteContext.Equipments
                        .Reverse()
                        .Include(x => x.CostCenter)
                        .Include(x => x.EqInfo)
                        .Skip(e.StartIndex)
                        .Take(e.PageSize)
                        .ToListAsync());
                }


But the following error is shown:

Quote:
: 'The LINQ expression 'DbSet<equipment>()
.Reverse()' could not be translated. Additional information: 'Reverse' could not be translated to the server because there is no ordering on the server side. 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.'
Posted
Updated 1-Aug-22 23:57pm

If you want to display the last page, retrieve the last page: .TakeLast( e.PageSize );

Enumerable.TakeLast<TSource>(IEnumerable<TSource>, Int32) Method (System.Linq) | Microsoft Docs[^]
 
Share this answer
 
Comments
Code4Ever 30-Jul-22 12:46pm    
But, using .TakeLast() causes runtime error. It cannot be translated.
You can't "reverse the order" of that data source, because you haven't defined any order for it. The data will be returned in whatever order the SQL engine chooses to load it. Whilst that might appear to be stable at the moment, it can and will change based on the indexes on your table, the amount of data in your table, the fragmentation of the indexes...

The only way to guarantee that data is returned in a particular order is to specify the order you want it to be returned in. If you want it sorted by "the last data added", then you either need to store the date and time when the record was added at the point when you add it, or you need to store an auto-generated number from a monotonically-increasing sequence when the record is added.

If you don't have any way of identifying when a record was added other than the order in which an unordered select currently returns them, then you have no way of displaying them in the order in which they were added.
 
Share this answer
 
Comments
Code4Ever 2-Aug-22 11:39am    
I solved it by adding .OrderByDescending(x => x.EquipmentId)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900