Entity Framework - attribute IN Clause usage

2020-01-27 04:01发布

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条回答
老娘就宠你
2楼-- · 2020-01-27 04:17
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

查看更多
登录 后发表回答