【问题标题】:How can I do this Eager Loading (using the .Include() method) in this code?如何在此代码中执行此急切加载(使用 .Include() 方法)?
【发布时间】:2009-11-23 08:14:38
【问题描述】:

我正在使用一个非常简单的存储库,使用的是 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
            });
 }

有人可以帮忙吗?

【问题讨论】:

    标签: .net entity-framework eager-loading


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多