Not In Linq Sql Entity Framework

Asked

Viewed 156 times

2

I need to do Nessa research, but presents 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();
} 

1 answer

3


The problem is that when you do db.Notifications.Select(o => o.EntityId) the return is a int (assuming this is the kind of EntityId) and you’re applying the Where on the return in Select. Therefore, the argument o within the Where is a int and not a complex guy.

Other than that, you can’t apply a Contains of a int within a list of a complex type. Possibly you want to compare some property.

Just change the order of the Select and of Where and fix the Contains (explain below) should solve. With more details, I can improve the response.

The Contains needs to be exchanged for Any, because he receives an element and not a Func<T, bool>. You can see more in this publication.

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 so

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

    Perfect. Any worked. Thanks @jbueno

Browser other questions tagged

You are not signed in. Login or sign up in order to post.