Entity Framework - attribute IN Clause usage

走远了吗. 提交于 2019-12-28 02:00:08

问题


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.

This is the approach:

Database table

Licenses
-------------
license INT
number INT
name VARCHAR
...

desired SQL Query in EF

SELECT * FROM Licenses WHERE license = 1 AND number IN (1,2,3,45,99)

EF Code

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.

How to do this query in EF?


回答1:


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



来源:https://stackoverflow.com/questions/13342817/entity-framework-attribute-in-clause-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!