LINQ to Entities 无法识别方法 'Boolean Exists(System.Predicate`1[Entities.Connection])' 方法
LINQ to Entities does not recognize the method 'Boolean Exists(System.Predicate`1[Entities.Connection])' method
我试图创建满足两个条件之一的笔记列表。
1.与创建的用户匹配。
2. OR 链接到连接。
使用下面的代码 returns 例外。我知道这个异常对于 Linq to entitites 表达式来说很常见。但我的问题是可以使用什么替代方法来代替 Exists?
"LINQ to Entities does not recognize the method 'Boolean Exists(System.Predicate`1[Entities.Connection])' method, and this method cannot be translated into a store expression."
_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() 将 return 为真。
我试图创建满足两个条件之一的笔记列表。 1.与创建的用户匹配。 2. OR 链接到连接。
使用下面的代码 returns 例外。我知道这个异常对于 Linq to entitites 表达式来说很常见。但我的问题是可以使用什么替代方法来代替 Exists?
"LINQ to Entities does not recognize the method 'Boolean Exists(System.Predicate`1[Entities.Connection])' method, and this method cannot be translated into a store expression."
_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() 将 return 为真。