views:

238

answers:

1

I'm having problems querying the entity model to get additional information.

My db has a Program table with a one to many relation with an Events table. The Entity model generates the relationships just fine, but I'm unable to figure out how to query the model to get the progam object with its events.

I can do this:

var foo = from program in entities.ProgramSet
          where program.StartDate > DateTime.now
          orderby program.StartDate
          select program;

No problems there. From what I've found on Microsofts Page (Shaping queries with Entity framework): msdn.microsoft.com/en-us/library/bb896272.aspx, if I wanted to get the child objects, I just do the following:

// Define a LINQ query with a path that returns 
// orders and items for a contact.
var contacts = (from contact in context.Contact
          .Include("SalesOrderHeader.SalesOrderDetail")
          select contact).FirstOrDefault();

However, there is no .Include or Include that I can find on the query.

Any suggestion? I know that I can do a foreach across the results, then run a .Events.Load() on it, but doesn't that force the IQueriable result to execute the sql, instead of optomize it to run only when a .ToList() etc is called on it?

Here is some sample code from my project:

public class ProgramRepository : CT.Models.IProgramRepository
{
    CTEntities db = new CTEntities();

    public IQueryable<Program> FindAllPrograms()
    {

        return db.ProgramSet;
    }
    public IQueryable<Program> FindUpcomingPrograms()
    {

        var programs =  from program in FindAllPrograms()

               where program.StartDate > DateTime.Now
               orderby program.StartDate                                
               select program;
        return programs;


    }

With the FindUpComingPrograms I would like to have it also include the Events Data. There is a relationship between the Program and Events model. Program has a List<Events> property, that I would like to fill and return with the IQueryable method.

Thanks again!

+1  A: 

The Include Function is part of the ObjectQuery object...

I think you are going to need to re-write your query to look something like this:

var contacts = context.Contact.Include("SalesOrderHeader.SalesOrderDetail").FirstOrDefault(); 

//Not sure on your dot path you might have to debug that a bit

Here is an Article that has some examples...

J.13.L
I guess, what I'm looking for is how do I, in the context of the Entity framework, query the Set, and get it's related children, so that the object will fill the list<T> property of the related class? And do this in the manner that will return it as an IQueriable<T>?Thanks
Mike
I've also found that I can go through and use a CreateSourceQuery() to get the referenced data that I need, or go to Microsofts Website: http://code.msdn.microsoft.com/EFLazyLoading and install the LazyLoading, so that when I access Program.Events it'll load the events right then and there, instead of right away.
Mike