get last element with linq to sql
I have this:
var result = (from t in MyDC
where t.UserID == 6
orderby t.UpdateTime
select t.ID).Las开发者_如何学Pythont();
Basically, I'm using Linq-to-Sql and it doesn't support the .Last operator. I could retrieve all the records of the user and then using linq to objects to get my value but I'm wondering how to do this with linq-to-sql and return only one record.
Thanks for your suggestions.
Just order by descending and use .First()
instead:
var result = (from t in MyDC
where t.UserID == 6
orderby t.UpdateTime descending
select t.ID).First();
If you don't know what you are sorting by, you could also use .Skip() and .Take() to reduce number of rows being returned
精彩评论