Include Entity Framework with a condition

conditional entity-framework include

Question

I must filter a dealer using both the dealer's ID and the unfinished checkins.

At first, it just returned the dealer based on id:

    // TODO: limit checkins to those that are not complete
    return this.ObjectContext.Dealers
        .Include("Groups")
        .Include("Groups.Items")
        .Include("Groups.Items.Observations")
        .Include("Groups.Items.Recommendations")
        .Include("Checkins")
        .Include("Checkins.Inspections")
        .Include("Checkins.Inspections.InspectionItems")
        .Where(d => d.DealerId == id)
        .FirstOrDefault();

As you can see, limiting checkins is necessary. I did as follows:

var query = from d in this.ObjectContext.Dealers
                            .Include("Groups")
                            .Include("Groups.Items")
                            .Include("Groups.Items.Observations")
                            .Include("Groups.Items.Recommendations")
                            .Include("Checkins.Inspections")
                            .Include("Checkins.Inspections.InspectionItems")
                            .Where(d => d.DealerId == id)
                        select new
                        {
                            Dealer = d,
                            Groups = from g in d.Groups
                                     select new
                                     {
                                         Items = from i in g.Items
                                                 select new
                                                 {
                                                     Group = i.Group,
                                                     Observations = i.Observations,
                                                     Recommendations = i.Recommendations
                                                 }
                                     },
                            Checkins = from c in d.Checkins
                                       where c.Complete == true
                                       select new
                                       {
                                           Inspections = from i in c.Inspections
                                                         select new
                                                         {
                                                             InspectionItems = i.InspectionItems
                                                         }
                                       }

                        };

            var dealer = query.ToArray().Select(o => o.Dealer).First();

            return dealer;

It works. However, I'm not certain I'm doing morally.

What is the most effective approach to carry out what I did? maybe a stored procedure?

I doubt that I will ever again need to employ the Include clause.

I'm grateful.

1
4
7/14/2011 9:47:28 PM

Accepted Answer

Perhaps you could utilize the EF ObjectContexts' relation fixup technique. Multiple searches for items connected by associations are answered when they are made in the same context. With each side having navigation characteristics and your association between Dealers and Checkins being 1:n, you might perform things like:

var dealer = yourContext.Dealers
             .Where(p => p.DealerId == id)
             .FirstOrDefault();
if(dealer != null)
{
    yourContext.Checkins
           .Where(c => c.Complete && c.DealerId == dealer.DealerId)
           .ToList();

I haven't tried this yet, but EF creates equivalent references since it recognizes that the Checkins the second query's context adds belong to the dealer from the first query.

2
7/14/2011 10:25:57 PM

Popular Answer

You must perform this projection if you wish to load filtered relations with a single query, but you don't need to make those calls toInclude Once you start creating forecasts, including them is not necessary since you have returning data in your possession.

Because stored procedures run using Entity framework are unable to populate linked entities, they will only be helpful if you switch back to ordinary ADO.NET (only flattened structures).

The automatic fixup mentioned by @Andreas requires multiple database queries, and as far as I'm aware it only functions if lazy loading is turned off because proxied objects mysteriously lack fixup information and still have internal flags indicating that each relation is not loaded, which causes additional database queries to be executed when you access them for the first time.



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