2009-11-23 37 views
0

我有一个非常简单的资源库,我在玩VS2010 Beta 2的Entity Framework v4。如何在此代码中执行此Eager加载(使用.Include()方法)?

我试图动态地包含Include方法,如果用户可选地请求它。

例如。

Public IQueryable<Foo> GetFoos(bool includeBars) 
{ 
    var entites = new Entities("... connection string ... "); 

    var query = from q in entities.Foos 
       select q; 

    if (includeBars) 
    { 
     // THIS IS THE PART I'M STUCK ON. 
     // eg. query = from q in query.Include("Bars") select q; 
    } 

    return (from q in query 
      select new Core.Foo 
      { 
       FooId = q.FooId, 
       CreatedOn = q.CreatedOn 
      }); 
} 

任何人都可以请帮忙吗?

回答

3

你做得很好,只是你必须从IQueryable的投“查询”来的ObjectQuery:

query = from q in ((ObjectQuery<Foo>)query).Include("Bars") select q; 

不知道这是好主意,做LINQ查询里面的演员,但我认为你会明白需要做什么。