我有一个非常简单的存储库,我正在使用VS2010 Beta 2附带的Entity Framework v4。
我正在尝试动态地包含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
});
}
有人可以帮忙吗?
答案 0 :(得分:3)
你做得很好,只需要将IQueryable中的“查询”转换为ObjectQuery:
query = from q in ((ObjectQuery<Foo>)query).Include("Bars") select q;
不确定在linq查询中进行转换是否是个好主意,但我认为你会明白需要做什么。