如何在此代码中执行此预加载(使用 .Include() 方法)?
我正在使用一个非常简单的存储库,使用 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
});
}
有人可以帮忙吗?
I have a very simple repository I'm playing around with, using Entity Framework v4 that comes with VS2010 Beta 2.
I'm trying to dynamically include the Include method, if a user optionally asks for it.
eg.
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
});
}
Can anyone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你做得很好,只是你必须将“查询”从 IQueryable 转换为 ObjectQuery:
不确定在 linq 查询内部进行转换是否是个好主意,但我认为你会明白需要做什么。
You are doing good, just you would have to cast "query" from IQueryable to ObjectQuery:
Not sure if it's good idea to do the cast inside of linq query, but I think you will get the point what need to be done.