Something like (appended at the end of your query):
select new {
g.Key,
Product = g.OrderByDescending(x => x.CreatedOn).First()
}
should do the job; or perhaps more conveniently:
select new {
g.Key.ProductId,
g.Key.Name,
Product = g.OrderByDescending(x => x.CreatedOn).First()
}
Or if you don't need the key explicitly (because it is available under the item anyway), then just:
select g.OrderByDescending(x => x.CreatedOn).First()
Basically, each g
is an IGrouping<T>
(for some anonymous T
), which means each g
has a .Key
property that is your group, and each g
is an IEnumerable<>
sequence of the original elements (presumably Product
).