Last 10 items from Firebasedatabase in Xamarin Forms

Bas H 141 Reputation points
2022-12-07T18:50:36.143+00:00

With this i get all the items from a Firebasedatabase Decending by BalId to CollectionView or ListView.

public async Task<List<Fotos>> GetAllFotosLast()  
        {  
            try  
            {  
                await Task.Delay(2000);  
                return (await firebase  
                  .Child("Foto/")  
                  .OnceAsync<Fotos>()).Select(item => new Fotos  
                  {  
                      BalId = item.Object.BalId,  
                      RollNo = item.Object.RollNo,  
                      Foto = item.Object.Foto,  
                      Titel = item.Object.Titel,  
                      Fototekst = item.Object.Fototekst  
                  }).OrderByDescending (x => x.BalId).ToList();  
          }  
            finally  
            {  
            }     
        }  

But how do i get the last 10 items from this List Descending ?

Maybe with Limit 10 ????? but i does not recognise Limit in Xamarin Forms.

Is this possible to make ik work or do i have to try another way

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,380 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2022-12-08T06:20:24.823+00:00

    Hello @Bas H ,

    how do i get the last 10 items from this List Descending

    You could descend the list according to the BalId, then take 10 objects, refer to the following code :

    List<Fotos> fooList2 = fooList.OrderByDescending(x => x.BalId).Take(10).ToList();  
    

    But it's not clear that if you want to get the last 10 items from the obtained list. If so, you can try the following code:

      List<Fotos> fooList3 = fooList.Skip(Math.Max(0, fooList.Count - 10)).ToList();// fooList is the list you get from database  
    

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.