SQL"不"语法实体框架4.1 [英] SQL "not in" syntax for Entity Framework 4.1
问题描述
我有实体框架语法一个简单的问题,为不是在SQL等价的。从本质上讲,我想下面的SQL语法转换成实体框架语法:
I have a simple issue with Entity Framework syntax for the "not in" SQL equivalent. Essentially, I want to convert the following SQL syntax into Entity Framework syntax:
select ID
from dbo.List
where ID not in (list of IDs)
下面是我用于查找单个记录的方法:
Here is a method that I use for looking up a single record:
public static List GetLists(int id)
{
using (dbInstance db = new dbInstance())
{
return db.Lists.Where(m => m.ID == id);
}
}
下面是一个伪的方法,我想用这个:
Here is a pseudo-method that I want to use for this:
public static List<List> GetLists(List<int> listIDs)
{
using (dbInstance db = new dbInstance())
{
return db.Lists.Where(**** What Goes Here ****).ToList();
}
}
谁能给我指针作为在其中,
子句地区发生的事情?我看了这个一些论坛,并且看到了。载有()
或。任何()
,但没有提及例子是一个足够接近配合。
Can anyone give me pointers as to what goes in the Where
clause area? I read some forums about this and saw mention of using .Contains()
or .Any()
, but none of the examples were a close enough fit.
推荐答案
给这个一去...
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();
}
}
这些将变成约以下数据库查询:
These will turn into approximately the following database queries:
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>)
分别。
这篇关于SQL&QUOT;不&QUOT;语法实体框架4.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!