LINQ Query to inner join DbContext Entities to IQueryable

c# entity-framework linq

Question

I have two tables with a one-to-many relationship: "Events" and "Services" (each event has at least one service).

I'm creating a C# console application that will extract the services that correlate to a subset of the events.

I've got a technique that turns the occurrences into anIQueryable<Event> object, and it functions as intended. However, once I do thatIQueryable<Event> opposed to anIQueryable<Service> (As depicted below) my finished object refers to multiple contexts, but I just want it to include theIQueryable<Service> results.

Exists a more effective approach to this?

My "ExtractServices" technique is as follows:

public IQueryable<Service> ExtractServices(IQueryable<Event> events)
{
    using (var preCertEntities = new PreCertEntities())
    {
        IQueryable<Service> services = from s in preCertEntities.Services
                                       orderby s.EventId
                                       select s;

        services = from s in services
                   join e in events on s.EventId equals e.EventId
                   select s;

        return services;
    }
1
0
5/9/2013 6:34:15 PM

Accepted Answer

Since EF takes care of it for you, you shouldn't need to extract child relationships in this manner:

public IQueryable<Service> ExtractServices(IQueryable<Event> events)
{
    return events.SelectMany(e => e.Services).OrderBy(s => s.EventId);
}
2
5/9/2013 6:38:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow