实体框架 - 属性IN子句用法 [英] Entity Framework - attribute IN Clause usage
本文介绍了实体框架 - 属性IN子句用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过我的数据库中的查询中使用正常WHERE和IN子句的各种字段过滤一些实体,但是我不知道如何使用EF。
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.
这是一种方法:
数据库表
Licenses
-------------
license INT
number INT
name VARCHAR
...
EF中所需的SQL查询
SELECT * FROM Licenses WHERE license = 1 AND number IN (1,2,3,45,99)
EF代码
using (DatabaseEntities db = new DatabaseEntities ())
{
return db.Licenses.Where(
i => i.license == mylicense
// another filter
).ToList();
}
我已尝试过任何和容器,但我不知道该怎么做如果在EF中执行此查询,
I have tried with ANY and CONTAINS, but I do not know how to do that with EF.
推荐答案
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();
}
应该工作
这篇关于实体框架 - 属性IN子句用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文