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.


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.


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

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


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 =>


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

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


var lastdate = db.table.orderby(a =>;


call first().
for example:

lastdate = 
    (from a in db.table
     orderby descending

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