kitty sarvaj kitty sarvaj - 2 months ago 18
SQL Question

LINQ get the latest status

i got two table in SQL, one is user table and another is status table,i want to get the latest status to display using LINQ.

/// User Table//
userID (Pk) userName telephonenumber
1 xx1 xxxxxxxx
2 xx2 xxxxxxxx2
3 xx3 xxxxxxxx3
4 xx4 xxxxxxxx4


statusID(pk) userID statusMessage createdAt
1 2 ddddddddx 2012-09-18 13:30:08
2 4 zzzzddddd 2012-09-18 13:30:10
3 1 ssswessss 2012-09-18 16:30:10
4 2 ddddddddd 2012-09-18 16:30:15
5 4 xxxxxweew 2012-09-18 17:30:18

Answer

Assuming Statuses is name of your second table, you can use:

dbContext.Statuses.OrderBy(a => a.createdAt).Last();

You can add some filter (ex. userID) but it is not specified in question.


Update 1, filter by specific userID

dbContext.Statuses.Where(a => a.userID = someID)
                  .OrderBy(a => a.createdAt)
                  .Last();
Comments