Accepted answer

since you are always going to be returning all of the items in the table, why not just make a recursive method that gets all children for a parent and then use that on the in-memory items:

partial class items
    public ienumerable<item> getallchildren()
        //recursively or otherwise get all the children (using the hierarchy navigation property?)


var items = 
    from item in items.tolist()
    group new 
    } by item.itemid;

sorry for any syntax errors...


well, if the hierarchy is strictly 2 levels you can always union them and let linq sort out the sql (it ends up being a single trip though it needs to be seen how fast it will run on your volume of data):

var hlist = from h in hierarchies
            select new {h.parent, h.child};

var slist = from h in hierarchies
            join h2 in hlist on h.parent equals h2.child
            select new {h2.parent, h.child};

hlist = hlist.union(slist);

this gives you an flat ienumerable<{item, item}> list so if you want to group them you just follow on:

var glist = from pc in hlist.asenumerable()
            group pc.child by pc.parent into g
            select new { parent = g.key, children = g };

i used asenumerable() here as we reached the capability of linq sql provider with attempting to group a union. if you try it against iqueryable it will run a basic union for eligable parents then do a round-trip for every parent (which is what you want to avoid). whether or not its ok for you to use regular linq for the grouping is up to you, same volume of data would have to come through the pipe either way.

edit: alternatively you could build a view linking parent to all its children and use that view as a basis for tying items. in theory this should allow you/l2s to group over it with a single trip.

More Query from same tag