实体框架 - 属性 IN 子句用法 [英] Entity Framework - attribute IN Clause usage

查看:29
本文介绍了实体框架 - 属性 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();
}

我尝试过使用 ANY 和 CONTAINS,但我不知道如何使用 EF.

I have tried with ANY and CONTAINS, but I do not know how to do that with EF.

如何在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆