首页 > 解决方案 > 为什么不能在 Include 内的 Where 方法中使用父实体的属性值?

问题描述

我需要查询子实体的创建日期与父实体相同的数据。所以,我尝试了以下代码:

context.Parent.Include(p => p.Child.Where(c => c.CreatedDate == p.CreatedDate)).ToList();

但它会引发无法翻译 LINQ 表达式的错误。我怎样才能实现这个功能或者有什么办法吗?

标签: asp.net.netentity-framework.net-coreentity-framework-core

解决方案


你能做这样的事情吗:?

context.Parent.Include(p => p.Child.Where(c => c.CreatedDate == c.Parent.CreatedDate)).ToList();

从使用旧版本的 EF 开始,我假设您不知道Where 方法p中的类型实例Parent


推荐阅读