Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Order By to select lowest Index

I am relatively new to LINQ and don't know how to do an Order By. I have an IEnumerable list of myObject and want to do something like select first myObject from myObjectList order by myObject.id asc How can I accomplish this? Thanks

like image 749
MikeTWebb Avatar asked Sep 30 '10 19:09

MikeTWebb


Video Answer


1 Answers

Ascending is the default order by direction.

var query = from myObject in myObjectList
            orderby myObject.id
            select myObject;

Object o = query.FirstOrDefault();

If you want descending, you will want to use orderby myObject.id descending.

like image 60
villecoder Avatar answered Sep 22 '22 01:09

villecoder