Use OrderBy
for the ordering, and First
or possibly FirstOrDefault
for the equivalent of TOP 1
:
var session = db.Sessions.OrderBy(x => x.StartTime).FirstOrDefault();
if (session != null)
{
// Use the session
}
else
{
// There weren't any sessions
}
You could use a query expression for the first part, but it seems pretty pointless – it would end up being more long-winded than using “dot notation”, so I’d stick to that.
solved SQL query to linq to SQL query