SQL 实体框架 - 属性 IN 子句用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13342817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework - attribute IN Clause usage
提问by unairoldan
I need to filter some Entities by various fields using "normal" WHERE and IN clauses in a query over my database, but I do not know how to do that with EF.
我需要在对我的数据库的查询中使用“普通”WHERE 和 IN 子句按各种字段过滤一些实体,但我不知道如何使用 EF 执行此操作。
This is the approach:
这是方法:
Database table
数据库表
Licenses
-------------
license INT
number INT
name VARCHAR
...
desired SQL Query in EF
EF 中所需的 SQL 查询
SELECT * FROM Licenses WHERE license = 1 AND number IN (1,2,3,45,99)
EF Code
EF代码
using (DatabaseEntities db = new DatabaseEntities ())
{
return db.Licenses.Where(
i => i.license == mylicense
// another filter
).ToList();
}
I have tried with ANY and CONTAINS, but I do not know how to do that with EF.
我已经尝试过 ANY 和 CONTAINS,但我不知道如何使用 EF。
How to do this query in EF?
如何在 EF 中执行此查询?
回答by Albin Sunnanbo
int[] ids = new int[]{1,2,3,45,99};
using (DatabaseEntities db = new DatabaseEntities ())
{
return db.Licenses.Where(
i => i.license == mylicense
&& ids.Contains(i.number)
).ToList();
}
should work
应该管用