我试图创建满足两个条件之一的笔记列表。 1.与创建的用户匹配。 2. OR 链接到连接。
使用以下代码返回异常。我知道这个异常对于 Linq to entitites 表达式来说很常见。但我的问题是可以使用什么替代方法来代替 Exists?
“LINQ to Entities 无法识别‘Boolean Exists(System.Predicate`1[Entities.Connection])’方法,并且无法将此方法转换为存储表达式。”
_context.Notes
.Include(t => t.Connections)
.Where(t => t.CreatedUserId == userId || t.Connections.ToList().Exists(c => c.UserId == userId))
最佳答案
这里的问题是 Entity Framework 不理解您的 C# 代码并且无法解析 .Exists()。
另一种写法如下:
_context.Notes
.Include(t => t.Connections)
.Where(t => t.CreatedUserId == userId || t.Connections.Any(c => c.UserId == userId));
这里,如果任何连接的 UserId 等于 userId,.Any() 将返回 true。
关于c# - LINQ to Entities 无法识别方法 'Boolean Exists(System.Predicate` 1[Entities.Connection])' 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40340774/