I have this:
var result = (from t in MyDC
where t.UserID == 6
orderby t.UpdateTime
select t.ID).Last();
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.
Get the last element from a sequence using the Linq Last() method. The following is our array. int[] val = { 10, 20, 30, 40 }; Now, get the last element.
The Last<TSource>(IEnumerable<TSource>) method throws an exception if source contains no elements. To instead return a default value when the source sequence is empty, use the LastOrDefault method.
MaxBy(p => p. Age);
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With