2010-10-08 44 views
-1

有没有办法急于加载在查询中获取的实体的子集合,而无需在Expand方法中指定集合的​​路径为string在查询执行期间急切地加载收集属性

目前,我有以下几点:

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 
     } 
    } 

有什么办法避免把string S IN的.Expand方法,或者是反映在我的代码我唯一的,以避免产生复制/粘贴未经检查的编译器的脆弱性?

回答

0

我唯一的当前解决方案使用反射来构建string.Expand方法的路径。

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 
    } 
} 

(该IsSubclassOfRawGenericCollection方法是围绕IsSubclassOfRawGeneric方法的包装从Jared Par on SO