score:4

Accepted answer

you can try with recursion

public static list<locationviewmodel> gethierarchy(list<linkparentchildviewmodel> linkparentchildviewmodels, int parentid)
{
    return linkparentchildviewmodels.where(x => x.parent.id == parentid).select(x => new locationviewmodel
    {
        id = x.parent.id,
        code = x.parent.code,
        childlocations = gethierarchy(linkparentchildviewmodels, x.child.childlocationid)
    }).tolist();
}

call this from main method

var result = gethierarchy(linksparentchild, 8);

Related Query

More Query from same tag