Entity Framework Include with condition

后端 未结 4 1256
情歌与酒
情歌与酒 2020-12-11 07:51

I need to filter a dealer based on id and the uncomplete checkins

Initially, it returned the dealer based only on id:

    // TODO: limit checkins to          


        
4条回答
  •  轮回少年
    2020-12-11 08:44

    Your accepted solution will generate multiple database queries. As Ladislav Mrnka said a projection is the only way to pull your result with one query. The maintance of your code indeed hard. Maybe you could use an IQueryable-Extension that builds the projection dynamically and keep your code clean:

    var query = this.ObjectContext.Dealers.SelectIncluding( new List>>>(){
    
        x => x.Groups,
        x => x.Groups.Select(y => y.Items),
        x => x.Groups.Select(y => y.Items.Select(z => z.Observations)),
        x => x.Groups.Select(y => y.Items.Select(z => z.Recommendations)),
        x => x.Checkins.Where(y => y.Complete==true),
        x => x.Checkins.Select(y => y.Inspections),
        x => x.Checkins.Select(y => y.Inspections.Select(z => z.InspectionItems))
    
    });
    var dealer = query.First();
    return dealer;
    

    You can find the extension at thiscode/DynamicSelectExtensions on github

提交回复
热议问题