按子实体元素筛选查询

本文关键字:筛选 查询 元素 实体 | 更新日期: 2023-09-27 18:08:14

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

任何帮助都将非常感激。谢谢。

按子实体元素筛选查询

问题似乎是Include下的Select语句。您可以尝试删除它,并添加一个额外的条款到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();