1
votes

Let's say I have Category > SubCategory > SubSubCategory > Item set up in my EF entities.

What is the best way to get Category, Subcategory, SubSubCategory and Item where Item.Property = x all in one single request to the server using WCF RIA Services?

With .Include I can only get the children of the entity, not grandchildren and further down ( or up depending on how you look at it).

Furthermore, if I do this...

 public IQueryable<ToolingTreeItem> GetTree(int currentLocationId)
    {
        var tree = from tc in this.ObjectContext.ToolingCategories
                   from tg in tc.ToolingGroups
                   from tt in tg.ToolingTypes
                   from t in tt.Toolings
                   where t.CurrentLocationId == currentLocationId
                   select new ToolingTreeItem { Cat = tc, Group = tg, Type = tt, Tool = t };

        return tree;
    }

...the method is not available on my context in the client side project, presumably because my custom entity class ToolingTreeItem is not recognized somewhere in the mysteries of the deep chasm that is WCF RIA Services.

If it isn't obvious by now, all I want to do is populate my TreeView with Category > SubCategory > SubSubCategory > Item in a single call to the server. What is the best approach?

Many happy returns!

2
Can you post the ToolingTreeItem class definition, what attributes did you use? RIA expects [Serializable] attribute and [DataMember] on serialized members.almog.ori
What if to start the query from the bottom: ObjectContext.Toolings.Include("ToolingType.ToolingGroup.ToolingCategory").Where(t => t.CurrentLocationId == currentLocationId).ToList();vortexwolf

2 Answers

1
votes

You should be able to load the entities by using eager loading. Assuming that you add "[Include]" to the "parent" attributes in you metadata something similar to the code below should work (note that I have guessed the name of all relations so you will probably need to edit the code)

public IQueryable<Toolings> GetToolsWithTree(int currentLocationId) 
{ 
    var tree = from t in this.ObjectContext.Tooling.Include("ToolingType.ToolingGroup.ToolingCategory")
               where t.CurrentLocationId == currentLocationId
               select t; 

    return tree; 
} 
0
votes

It looks like ToolingTreeItem is a complex object, rather than an entity. Unfortunately, RIA services can't generate classes on the client side that are a mix of complex objects and entities - the class has to be entirely one or the other. The two solutions that I'm aware of are to make the ToolingTreeItem an 'entity', by putting the Key attribute on a property, or just make several requests for the data. I've also found this a real limitation.