Using join tables

2 posts, 0 answers
  1. Stefan Lorenz
    Stefan Lorenz avatar
    13 posts
    Member since:
    May 2010

    Posted 02 Jun 2010 Link to this post

    Hi there,

    I got stuck in working with many-to-many relations in combination with OA/LinQ and need some advice on it. I've read the docs and forums but can't find (or see) a solution, although I guess this is a daily business scenario:

    I have two persistent classes named Users and Groups, each containing a IList<> of the other type. A user belongs to 0-n groups and a group has 0-n members. Nothing special so far. Using forward mapping I setup a managed collection and my DB contains a join table Groups_Users containing both table's PKs, exactly the way I would have created my tables manually. Nice!

    Initially I want my UI to show a Gridview (Winforms) listing all my groups with their members in Child-Templates. My approach: I need to brake down my many-to-many relation into a 1:n relation from the groups point of view to feed the gridview's child template, So I thought a BindingList of anonymous types containing User+Group ID would fit as a datasource for the child template.

    In SQL I would have queried my list this way (stripped down to IDs):

    SELECT grp.groups_id, usr.users_id FROM groups grp
    LEFT OUTER JOIN Groups_Users rel ON (rel.groups_id=grp.groups_id)
    LEFT OUTER JOIN Users usr ON (usr.users_id=rel.users_id)

    With this as Child-Data I could relate to the master template using groups_id, so the gridview would work like intended (well, I guess so).

    So, a couple of questions arise here:
    -How to do this in LINQ? I can't do scope.Extent<Groups_Users>() as there isn't a Groups_Users class (or am I supposed to write one?), or is there another way to define the joins?
    - Am I heading in the right direction or is there a total different approach I should try? I tried to set all Users as Child datasource and create a relation using the Group.Members collection, but no luck so far.

    Thanks in advance for any help

    Stefan
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 04 Jun 2010 Link to this post

    Hello Stefan Lorenz,

    I am afraid that currently the RadGridView for Windows Forms cannot represent such hierarchy relations. However, our WinForms team is working on this and they will provide such possibilities in one of the following releases.
    In the meantime the only workaround that came to my mind is to use two grids and bind the second one depending on the selected item in the master grid. The binding would look like this:
    ...
    this.radGridView1.DataSource = scope.Extent<Group>().ToList();
    ...
     
    private void radGridView1_SelectionChanged(object sender, EventArgs e)
    {
        if(this.radGridView1.SelectedRows.Count> 0)
        {
            this.radGridView2.DataSource = ((Group)this.radGridView1.SelectedRows[0].DataBoundItem).Users;
        }
    }
    On the other hand the OpenAccess model seems fine and I do not think that you should change it in any way. Hope that helps.

    Sincerely yours,
    Alexander
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
Back to Top