views:

6659

answers:

3

Hi,

Is there anyway I can create a not in clause like I would have in SQL Server in Linq to Entities?

Cheers

Paul

A: 

Try:

from p in db.Products
where !theBadCategories.Contains(p.Category)
select p;

What's the SQL query you want to translate into a Linq query?

Justice
LINQ to Entities does not support the Contains operator.
tvanfosson
It supports the Except operator, as in your answer?
Justice
@Justice -- according to the docs, yes. It supports the Except extension that uses the default equality operator.
tvanfosson
"Most of the LINQ set methods are supported in LINQ to Entities queries, with the exception of those that use an EqualityComparer."
tvanfosson
Hey there Justice; the thought was reasonable, but unfortunately it isn't supported in EF. But you're in good company: there are 2 deleted answers saying exactly the same thing. And since it was a fair thing to try, I'll +1 to even it up ;-p (it was -1)
Marc Gravell
+7  A: 

Here is a link to the supported Set extensions in LINQ to Entities. It looks like it supports set difference via Except so you could do something like:

   var exceptionList = new List { "exception1", "exception2" };
   var query = myEntities.MyEntity
                         .Select( e => e.Name )
                         .Except( exceptionList );

This assumes a complex entity in which you are excluding certain ones depending on their names and want the names of the entities that are not excluded. If you wanted the entire entity, then you'd need to construct the exceptions as instances of the entity class such that they would satisfy the default equality operator (see docs).

tvanfosson
A: 

t-sql <> is equal to linq to Entities != so try this !=

Carlos Saldaña