Using EF4 and Linq
I have a data structure which looks like this:
parentid childid version
1 2 1
1 3 1
1 4 1
1 2 2
1 3 2
1 5 2
...
That is, I want to select the parentid, childid but only for the highest version, for every parentid there is in the database.
My first attempt was this:
var links = data
.GroupBy (link => link.parentid)
.Select(ig => ig.OrderByDescending(link => link.version).First())
.Select ( link => new ....... );
However, this obviously only selects one of the child id’s for each parent id..
In the sample data above I want to get the child ids 2,3,5 from version 2 for parent 1 that is..
I ended up using a subquery like this:
This way, I get all the parentid’s in the db with only the children with the max version for that specific parent.