我有一个简单的问题,即与“ not in” SQL等效项有关的实体框架语法。本质上,我想将以下SQL语法转换为Entity Framework语法:
select ID from dbo.List where ID not in (list of IDs)
这是我用于查找单个记录的方法:
public static List GetLists(int id) { using (dbInstance db = new dbInstance()) { return db.Lists.Where(m => m.ID == id); } }
这是我要用于此的伪方法:
public static List<List> GetLists(List<int> listIDs) { using (dbInstance db = new dbInstance()) { return db.Lists.Where(**** What Goes Here ****).ToList(); } }
谁能给我指点一下Where条款区域的内容吗?我阅读了一些有关此的论坛,并提到了使用.Contains()或的情况.Any(),但是没有一个例子非常合适。
Where
.Contains()
.Any()
快去…
public static List<List> GetLists(List<int> listIDs) { using (dbInstance db = new dbInstance()) { // Use this one to return List where IS NOT IN the provided listIDs return db.Lists.Where(x => !listIDs.Contains(x.ID)).ToList(); // Or use this one to return List where IS IN the provided listIDs return db.Lists.Where(x => listIDs.Contains(x.ID)).ToList(); } }
这些将大致变成以下数据库查询:
SELECT [Extent1].* FROM [dbo].[List] AS [Extent1] WHERE NOT ([Extent1].[ID] IN (<your,list,of,ids>))
或者
SELECT [Extent1].* FROM [dbo].[List] AS [Extent1] WHERE [Extent1].[ID] IN (<your,list,of,ids>)
分别。