框架Linq查询的问题

时间:2013-08-06 11:42:15

标签: c# linq entity-framework c#-4.0

代码:

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);

1 个答案:

答案 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);
}