Linq/EF, Eager loading and GROUP BY issues

前端 未结 2 1477
说谎
说谎 2021-01-04 10:02

I\'m having issues with GROUP BY and eager loading. I try to explain what im doing. I\'m querying a datacontext ctx for events

The event class has the following pr

相关标签:
2条回答
  • 2021-01-04 10:22

    Thnx for the quick answer. You pointed me in the right direction. Here is the solution i came up with:

    using MyFunc = Func<ExtendedCoreContext, Guid, IQueryable<DataNotificationEvent>>;
    
    private static readonly MyFunc GetMentionsNewCompiledQuery = 
    
        CompiledQuery.Compile<ExtendedCoreContext, Guid, IQueryable<DataNotificationEvent>>(
    
            (ctx, personId) => ((ObjectQuery<DataNotificationEvent>)(
                from x in (
                    from e in ctx.Events.OfType<DataNotificationEvent>()
                    group e by e.SubjectId
                    into g
                    select g.OrderByDescending(p => p.Created).FirstOrDefault()
                )
                orderby x.Created descending
                where x.Targets.Any(t => t.Id == personId)
                select x
            ))
              .Include(EntityProperties.Event.TriggeredBy)
              .Include(EntityProperties.DataNotificationEvent.Targets)
    
        );
    
    0 讨论(0)
  • 2021-01-04 10:35

    You're projecting onto an anonymous type, so Include() isn't going to work like that. Because what you've done with the group and projecting into the anonymous type is to change the shape of the query. That tosses out the eager loading. Reading this article might help.

    0 讨论(0)
提交回复
热议问题