views:

72

answers:

2

I am trying to write a LINQ statement which selects only the rows in a table with the most recent rundate. For example, if I had a table with columns RunDate(datetime) and Value(decimal) one way of accomplishing this through sql might look like:

SELECT Value
FROM Table
WHERE RunDate = (SELECT MAX(RunDate) FROM Table)

Is there a way to do this in LINQ? Thank you

A: 
Table.OrderByDescending(row=>row.RunDate).Take(1); 
Ngu Soon Hui
I don't think that's the same thing... that will only give back one record, even if there are several rows with the same run date.
Aaronaught
+3  A: 

If you want the rows:

var rows = Table.Where(row => row.RunDate == Table.Max(r => r.RunDate));

If you just want the values:

var values = Table.Where(row => row.RunDate == Table.Max(r => r.RunDate))
                  .Select(row => row.Value);
Jason
thank you this worked perfectly...i'm brand new to LINQ if you couldn't tell
Andrew
We were all there at some point. We will all never go back. :-)
Jason