开发者

Aggregate in where clause of LINQ

开发者 https://www.devze.com 2022-12-16 16:32 出处:网络
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 V开发者_StackOverflow中文版al

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 V开发者_StackOverflow中文版alue(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


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);


Table.OrderByDescending(row=>row.RunDate).Take(1); 
0

精彩评论

暂无评论...
验证码 换一张
取 消