Entity Framework: how to do correct “Include” on custom type

后端 未结 2 1365
春和景丽
春和景丽 2021-01-19 17:22

Suppose we have 2 types, mapped to a Database via EF 4.

Schedule 1.....1 Visit

Also, we have third custom view type

public          


        
2条回答
  •  被撕碎了的回忆
    2021-01-19 17:51

    The error is spot on. Your trying to create an ObjectQuery (LINQ-Entities IQueryable), with a type (ScheduleView) which does not exist in your Model.

    You can project into this type, sure - but not affect the query with it.

    Will something like this work? (off the top of my head, untested)

    var scheduleViews = (from s in ctx.Schedules
                         join v in ctx.Visits.Include("Patient") 
                         on v.ScheduleId equals s.ScheduleId
                         select new ScheduleView 
                         { 
                            Schedule = s, 
                            Visit = v 
                         }).ToList();
    

    Your main problem is ObjectQuery is not translatable to a store expression. So do your include on the Visits entity set, not the IQueryable.

    HTH

提交回复
热议问题