Multiple Joins to Single Table

Posts   
 
    
Posts: 3
Joined: 18-Mar-2010
# Posted on: 18-Mar-2010 05:19:06   

I have a relationship where I need multiple joins back to a single table. I have tried to implement this using a relationship with an alias but can't access the Alias table fields in my datagrid.

Here is my code: RelationCollection relations = new RelationCollection(); relations.Add(PrivateGameEntity.Relations.ArmyEntityUsingArmyId, "Army"); relations.Add(PrivateGameEntity.Relations.ArmyEntityUsingOpponentArmyId, "OpponentArmy"); relations.Add(PrivateGameEntity.Relations.PlayerEntityUsingOpponentPlayerId, "Opponent");

    IPredicateExpression privateGameFilter = new PredicateExpression();
    privateGameFilter.Add(PrivateGameFields.PlayerId == Request.QueryString["PlayerId"].ToString());
    if (gameYearList.SelectedItem == null)
    {
        privateGameFilter.Add(PrivateGameFields.DatePlayed >= new DateTime(DateTime.Now.Year, 1, 1));
        privateGameFilter.Add(PrivateGameFields.DatePlayed <= new DateTime(DateTime.Now.Year, 12, 31));
    }
    else
    {
        privateGameFilter.Add(PrivateGameFields.DatePlayed >= new DateTime(Convert.ToInt32(gameYearList.SelectedValue), 1, 1));
        privateGameFilter.Add(PrivateGameFields.DatePlayed <= new DateTime(Convert.ToInt32(gameYearList.SelectedValue), 12, 31));
    }
    e.ContainedCollection.GetMulti(privateGameFilter, relations);

I then try to bind this to my Telerik RadGrid like this: <Telerik:GridHyperLinkColumn DataTextField="OpponentArmy.ArmyName" DataNavigateUrlFields= "OpponentArmyId" DataNavigateUrlFormatString= "~/public/armyprofile.aspx?ArmyId={0}" UniqueName="OpponentArmyName" HeaderText="Opponent Army" SortExpression="OpponentArmyName"></Telerik:GridHyperLinkColumn>

Any help would be appreciated I am new to this product and really struggling. Thanks

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 18-Mar-2010 08:04:45   

Joining to other entities/tables, doesn't necessarily mean their fields will be included in the select list.

When fetching an entityCollecttion, only the entity's fields are fetched, using a relation (Joining) would only help for filtering the returned entities.

Having said that, you have 2 options: 1- Use prefetchPaths, to fetch related entities as well, and so you can access their fields when binding to any control. 2- Fetch a flat list of fields from more than one table/entity joined together. And for that you'll have to use a DynamicList, or a pre-built TypedList.

Posts: 3
Joined: 18-Mar-2010
# Posted on: 18-Mar-2010 09:29:17   

Walaa wrote:

Joining to other entities/tables, doesn't necessarily mean their fields will be included in the select list.

When fetching an entityCollecttion, only the entity's fields are fetched, using a relation (Joining) would only help for filtering the returned entities.

Having said that, you have 2 options: 1- Use prefetchPaths, to fetch related entities as well, and so you can access their fields when binding to any control. 2- Fetch a flat list of fields from more than one table/entity joined together. And for that you'll have to use a DynamicList, or a pre-built TypedList.

Could you possibly provide and example including the binding as this is what I am really struggling with. In the previous code example I was able to bind to the field ArmyName which is in the Army table and mapped via the ArmyId. I just couldn't get the ArmyName field which is also on the Army table but mapped via the OpponentArmyId.

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 18-Mar-2010 11:36:54   

What worked with you, worked because of LazyLoading which is in-efficient in this particular case. Because one databse call would be called for each row of the Grid. And it will work that way of you get rid of the defined relations.

Anyway, Do you have these multiple relations to the Army Table defined in the Designer? If so then you should have more than one property representing the ArmyEntity.

And if you hadn't renamed them already to a more readable form, you would find them as follows: Army Army_ Army__ ....etc

Posts: 3
Joined: 18-Mar-2010
# Posted on: 21-Mar-2010 08:00:47   

Walaa wrote:

What worked with you, worked because of LazyLoading which is in-efficient in this particular case. Because one databse call would be called for each row of the Grid. And it will work that way of you get rid of the defined relations.

Anyway, Do you have these multiple relations to the Army Table defined in the Designer? If so then you should have more than one property representing the ArmyEntity.

And if you hadn't renamed them already to a more readable form, you would find them as follows: Army Army_ Army__ ....etc

Thanks for your advice Walaa, that last bit about the use of the _ was exactly what I had been looking for. I couldn't find this in the help. Thanks again.