views:

42

answers:

1

Is there a way to eagerly load the child collections of entities fetched in a query without having to specify the paths of the collections as strings in the Expand method?

Currently I have the following:

    foo_entities ctx = new foo_entities(new Uri("http://url/FooService.svc/"));
    ctx.MergeOption = MergeOption.AppendOnly;        
    var things = ctx.Things
                .Expand("ChildCollectionProperty1," +
                        "..." + 
                        "ChildCollectionPropertyN");
    foreach (var item in things)
    {
        foreach (var child in item.ChildCollectionProperty1)
        {
            //do thing
        }
    }  

Is there any way to avoid putting strings in the .Expand method, or is reflection my only out to avoid creating copy/paste unchecked by compiler fragility in my code?

A: 

My only current solution uses reflection to build the string or paths for the .Expand method.

foo_entitiesctx = new foo_entities(new Uri("http://url/FooService.svc/"));
ctx.MergeOption = MergeOption.AppendOnly;

var collections = from pi in typeof(TestResult).GetProperties()
                   where IsSubclassOfRawGenericCollection(pi.PropertyType)
                   select pi.Name;

var things = ctx.Things.Expand(string.Join(",", collections));
foreach (var item in things)
{
    foreach (var child in item.ChildCollectionProperty1)
    {
        //do thing
    }
}  

(The IsSubclassOfRawGenericCollection method is a wrapper around the IsSubclassOfRawGeneric method from Jared Par on SO)

jball