I have a table with products
Id | Name | Order
1 | product 1 | 5
1 | product 1 | 9
1 | product 1 | 2
2 | product 2 | 0
3 | product 3 | 1
I need to return just the product with the max order number:
Id | Name | Order
1 | product 1 | 9
2 | product 2 | 0
3 | product 3 | 1
tried this but it’s not working:
var items = (from i in db.products
group i by new
{
i.Id,
i.Name,
i.Order
} into g
select new
{
g.Key.Id,
g.Key.Name,
Order = g.Where(d => d.Order == g.Max(xx => xx.Order )).First().Order })
but it stills return all the 5 results.
thank you
Change
into
I think I would also rewrite the the last expression to
Mostly because I think it is easier to read, but the generated SQL might be more efficient too. But this is just a matter of taste and any performance difference can be verified with SQL Profiler.