c# - LINQ to Entities 无法识别方法 'Boolean Exists(System.Predicate` 1[Entities.Connection])' 方法

标签 c# entity-framework linq linq-to-entities

我试图创建满足两个条件之一的笔记列表。 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/

相关文章:

c# - 带有 'failed to load resource : the server responded with a status of 500' 的 Ajax 调用

entity-framework - 通用 Entity Framework 存储库的更新方法

c# - 这个无效的对象名称从何而来?

c# - "An expression tree may not contain an assignment operator"在 Select 子句中使用聚合

c# - 在一个类中声明静态方法并将其用作另一个类的方法

c# - 动态更改图像编码?

c# - DeleteAsync和PostAsync不起作用

c# - Principal Role App 引用的属性必须与 EntityType 的键完全相同

c# - 如何按日期排序前 10 个不同的行

C# Complex Linq-如何获取 ID 或子 ID 匹配的对象