我在我的例子中使用了实体框架。我想过滤子实体,但我得到异常' Include路径表达式必须引用在类型上定义的导航属性。使用虚线路径作为参考导航属性,使用Select运算符作为集合导航属性。'
public List<Notification> GetNotificationBySentDate(DateTime? dateTime)
{
if (dateTime == null)
{
return
_dbContext.Notifications.Include(x => x.Attachments.Select(a=>a.Clean==true))
.Where(x =>
x.Sent == null &&
x.FaultCount <= _appSettingsHelper.NotificationsFaultCountLimit &&
DbFunctions.AddSeconds(x.CreatedDate, x.DelaySeconds) < DateTime.UtcNow)
.OrderBy(a => DbFunctions.AddSeconds(a.CreatedDate, a.DelaySeconds))
.Take(_appSettingsHelper.NotificationsBySentStateSelectTop).ToList();
}
return _dbContext.Notifications.Include(x => x.Attachments).Where(x => x.Sent >= dateTime)
.OrderBy(a => a.CreatedDate)
.Take(_appSettingsHelper.NotificationsBySentStateSelectTop).ToList();
}
非常感谢任何帮助。感谢。
答案 0 :(得分:0)
问题似乎是Select
下的Include
声明。您可以尝试删除它并向Where
添加一个附加条款,例如&& x.Attachements.Clean == true
。因此,您的代码将是
_dbContext.Notifications.Include(x => x.Attachments)
.Where(x =>
x.Sent == null &&
x.FaultCount <= _appSettingsHelper.NotificationsFaultCountLimit &&
DbFunctions.AddSeconds(x.CreatedDate, x.DelaySeconds) < DateTime.UtcNow &&
x.Attachments.Clean == true)
.OrderBy(a => DbFunctions.AddSeconds(a.CreatedDate, a.DelaySeconds))
.Take(_appSettingsHelper.NotificationsBySentStateSelectTop).ToList();