Not In Linq Sql Entity Framework

2

I need to do this search, but it displays the error:

  

ambiguous invocation.

public List<Notification> Get(List<UserVisualization> item)
{
    return db.Notifications.Select(o=> o.EntityId).Where(o => !item.Contains(o.EntityId)).ToList();
} 
    
asked by anonymous 10.11.2016 / 19:12

1 answer

3

The problem is that when you do db.Notifications.Select(o => o.EntityId) the return is a int (assuming that this is the EntityId type) and you are applying Where to return Select . So the argument o within Where is int and not a complex type.

Otherwise, you can not apply a Contains of a int to a list of a complex type. Possibly you want to compare some property.

Just changing the order of Select and Where and setting Contains (explained below) should resolve. In more detail, I can improve the answer.

The Contains needs to be changed by Any , because it receives an element and not a Func<T, bool> . You can see more in this post.

return db.Notifications.Where(o => !item.Any(x => x.Propriedade == o.EntityId))
                       .Select(o => o.EntityId).ToList();

In fact, you can avoid having to reverse operations by doing this

return db.Notifications.Select(o => o.EntityId)
                       .Where(entityId => !item.Any(x => x.Propriedade == entityId));
    
10.11.2016 / 19:21