Grabbing Just The Top Entry From A LINQ Query

Posted by Soo on Stack Overflow See other posts from Stack Overflow or by Soo
Published on 2010-06-14T14:13:10Z Indexed on 2010/06/14 14:22 UTC
Read the original article Hit count: 172

Filed under:
|
|

I basically have a lot of poorly designed code to do something that, I'm sure, can be done far more elegantly.

What I'm trying to do is grab the last date from a database table.

var Result = 
from a in DB.Table
orderby a.Date descending
select new {Date = a};

foreach(var Row in Result)
{
LastDate = Row.Date.Date;
break;
}

Basically, there's a foreach loop that is designed to run only once. Crappy code! What's a "best practice" way to accomplish the same thing?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ