Reputation: 24899
I am drawing a blank on how to do this within Entity Framework query (other than in multiple steps)
Car table:
id (identity) | CarName | RegisteredOn | RegisteredBy
Basically i need to retrieve a distinct list of cars, each with the latest registered user.
If i have a table of cars, each car will be entered 1 or multiple times on different dates it was registered.
so CarA can be entered 5 times, with 3 owners, CarB can be entered 1 time and so on.
Let's say I want a query for 2/1/2011 to see all the cars in the table with the last registered name.
Is that easily achievable with Linq? would it require 2 queries, first to get list of all cars, 2nd to join in the latest registered user?
Upvotes: 2
Views: 2675
Reputation: 126547
Off the top of my head, something like...
var q = from c in Context.Cars
group c by c.Name into g
select g.OrderByDescending(gc => gc.RegisteredOn)
.FirstOrDefault();
Upvotes: 4