Eager loading of collection properties during query execution
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 string
s 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 string
s in the .Expand
method, or is r开发者_开发问答eflection my only out to avoid creating copy/paste unchecked by compiler fragility in my code?
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)
精彩评论