Say I have a table named Storage with records this kind
Item_Id | Date | Price
1 | 01/01/2011 | 50
1 | 03/26/2012 | 25
2 | 04/21/2012 | 20
3 | 08/02/2012 | 15
3 | 09/02/2012 | 13
4 | 10/02/2012 | 18
I would like to get the Item_Id with the latest Date associated to it (in this case it would be then "4") using linq to sql.
I'm using this expression
var lastDate= Storage
.GroupBy(s => s.Item_Id)
.Select(grp => new {
grp.Key,
LastDate = grp.OrderByDescending(
x => x.Date).First()
}).ToList();
and I'm getting a list with the latest date for each item_Id. So the next step would be to get the highest of all of them, but I'm stuck there.
Actually, eventually I'd need to skip some of the Item_Id's in the query but I guess I'll be able to add a where before the Select, something like Where(s => s.Item.Id {and here an expression that checks if it's inside the list I pass).
I'd appreciate any hint,
Thanks