Kinani
Kinani

Reputation: 484

Querying the latest entry in Azure Mobile Service

I am trying to query the latest insert made to the table, Azure Mobile Service adds by default the __createdAt column.

So, I am planning to sort the table according to that specific column, since __createdAt is a system property. I thought of adding it to my table model.

Now my question is: how to query this in C#?

Upvotes: 0

Views: 54

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87228

You can have a property in your model that tracks the __createdAt column, and sort based on that:

public class Person {
    public string Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    [CreatedAt] public DateTime CreatedAt;
}

And on the code:

var table = MobileService.GetTable<Person>();
var lastItems = await table
    .OrderByDescendent(p => p.CreatedAt)
    .Take(1)
    .ToEnumerableAsync();
var lastItem = lastItems.FirstOrDefault();

Upvotes: 1

Related Questions