LINQ to SQL in and not in

You use, where <list>.Contains( <item> )

var myProducts = from p in db.Products
                 where productList.Contains(p.ProductID)
                 select p;

Or you can have a list predefined as such:

int[] ids = {1, 2, 3};

var query = from item in context.items
            where ids.Contains( item.id )
            select item;

For the ‘NOT’ case, just add the ‘!’ operator before the ‘Contains’ statement.

Leave a Comment