Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities does not recognize the method 'Boolean Exists(System.Predicate`1[Entities.Connection])' method

I tried to create list of notes that satisfying either of two conditions. 1. Matched with the created user. 2. OR Linked with the connections.

using the below code returns an exception. I know this exception is common for Linq to entitites expression. But my question is what is the alternative methods can be used instead of 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))
like image 506
chenk Avatar asked Oct 31 '16 10:10

chenk


1 Answers

The issue here is that Entity Framework does not understand your C# code and cannot parse .Exists().

An alternative way of writing this would be as followed:

_context.Notes
    .Include(t => t.Connections)
    .Where(t => t.CreatedUserId == userId || t.Connections.Any(c => c.UserId == userId));

Here the .Any() will return true if any Connections have a UserId that is equal to userId.

like image 175
Adam T Avatar answered Nov 07 '22 02:11

Adam T