按子实体元素过滤查询

时间:2015-08-17 13:59:11

标签: c# entity-framework

我在我的例子中使用了实体框架。我想过滤子实体,但我得到异常' 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();
    }

非常感谢任何帮助。感谢。

1 个答案:

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