Entity Framework Include with condition

后端 未结 4 1258
情歌与酒
情歌与酒 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:24

    Maybe you can make use of the relation fixup mechanism in the EF ObjectContexts. When you do multiple queries in the same context for entities, that are related by associations, these are resolved. Assuming your association between Dealers and Checkins is 1:n with navigation properties on each side, you could do 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 have not tested this by now, but since EF recognises that the Checkins, it inserts into the context by the second query belong to the dealer from the first query, corresponding references are created.

提交回复
热议问题