我怎样才能做到这一点预先加载在这个代码(使用在.include()方法)?
-
21-09-2019 - |
题
我有一个非常简单的存储库,我玩弄,使用随VS2010 Beta 2的实体框架v4的。
我试图动态地包括包括方法,如果用户任选要求它。
例如
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
});
}
任何人都可以请帮助?
解决方案
您正在做的不错,只是你将不得不投“查询”从IQueryable的到的ObjectQuery:
query = from q in ((ObjectQuery<Foo>)query).Include("Bars") select q;
不知道这是好主意,做LINQ查询的演员里面,但我认为你会得到什么需要做的地步。
不隶属于 StackOverflow