如何在此代码中执行此预加载(使用 .Include() 方法)?

发布于 2024-08-11 23:45:10 字数 653 浏览 6 评论 0原文

我正在使用一个非常简单的存储库,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯到世界奔溃 2024-08-18 23:45:10

你做得很好,只是你必须将“查询”从 IQueryable 转换为 ObjectQuery:

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

不确定在 linq 查询内部进行转换是否是个好主意,但我认为你会明白需要做什么。

You are doing good, just you would have to cast "query" from IQueryable to ObjectQuery:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文