Implement “not in” (aka “not exists”) logic in LINQ

你离开我真会死。 提交于 2019-12-02 12:41:35

SQL to LINQ ( Case 7 - Filter data by using IN and NOT IN clause)

Note: IN and NOT IN use the same function in the LINQ query, but it just use a ! (not) symbol for it. Here is the graphical representation:

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:

var 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!