LINQ to SQL in and not in

前端 未结 3 1575
醉梦人生
醉梦人生 2020-11-29 09:44

What is in and not in equals in LINQ to SQL?

For example

select * from table in ( ...)
and 
select * from table not in (.         


        
3条回答
  •  执笔经年
    2020-11-29 10:21

    You use, where .Contains( )

    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.

提交回复
热议问题