代码:
IQueryable<Meet> query = _meetReadService.GetRecords()
.Include(x => x.MeetType)
.Where(x => x.EndDateTime <= DateTime.Now);
当我将以下行添加到上面的linq查询时,错误消息将被抛出为
Include路径表达式必须引用在类型上定义的导航属性。使用虚线路径作为参考导航属性,使用Select运算符作为集合导航属性。
x => x.MeetTeamMembers.Where(e => e.MeetType.IsManager)
.Select(z => z.User.Name)
.FirstOrDefault()
我的完整代码:
IQueryable<Meet> query = _meetReadService.GetRecords()
.Include(x => x.MeetType)
.Include(x => x.MeetTeamMembers.Where(e => e.MeetType.IsManager)
.Select(z => z.User.Name)
.FirstOrDefault())
.Where(x => x.EndDateTime <= DateTime.Now);
答案 0 :(得分:0)
您无法在对Include
的调用中进行过滤。这意味着.Where(e => e.MeetType.IsManager)
是罪魁祸首。您想要的只能在执行查询后完成:
IQueryable<Meet> query = _meetReadService.GetRecords()
.Include(x => x.MeetType)
.Include(x => x.MeetTeamMembers)
.Select(z => z.User.Name).FirstOrDefault())
.Where(x => x.EndDateTime <= DateTime.Now);
var queryResults = query.ToList();
foreach (var meet in queryResults)
{
meet.MeetTeamMembers = meet.MeetTeamMembers.Where(e => e.MeetType.IsManager);
}