score:13

Accepted answer
var first = result.first();

if the result set is empty, this will throw an exception; you can use firstordefault() which will return a null if the result set is empty.

score:0

you could also call result.take(1) the difference between take(1) and first() is that first returns a single object, and take returns an ienumerable of the type.

score:0

if date is a reference type then you may consider the coalesce operator.

var lastdate = result.firstordefault() ?? new date();

score:0

if you're trying to find the latest date, you could use the "max" function (which i believe linq2sql will convert to a sql max operation):

var maxdate = db.table.max(a => a.date)

score:1

firstordefault() and as a bonus, you can use lastordefault() for... you guessed it...

[edit] -- oh, sudden rush there with the same answer :)

score:3

var lastdate = db.table.orderby(a => a.date).firstordefault();

score:8

call first().
for example:

lastdate = 
    (from a in db.table
     orderby a.date descending
     select a.date
    ).first();

if the table might be empty, call firstordefault(), which will return datetime.minvalue instead of throwing an exception.


Related Query

More Query from same tag