views:

40

answers:

1

Hi folks,

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?

+3  A: 

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.

Misha N.