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
            });
 }

有人可以帮忙吗?

4

1 回答 1

3

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

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

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

于 2009-11-23T09:09:21.603 回答