Entity Framework - Employing Attributes in IN Clause Queries
In Entity Framework, filtering entities with complex criteria involving both WHERE and IN clauses can be achieved using specific techniques. Consider the task of filtering database records based on attributes within tables and employing an IN clause to match specific values.
To achieve this in Entity Framework, one suggests utilizing the Contains method. For instance, given the following tables:
Licenses ------------- license INT number INT name VARCHAR ...
The desired SQL query using Entity Framework can be represented as:
SELECT * FROM Licenses WHERE license = 1 AND number IN (1,2,3,45,99)
To create this query in EF, we can employ the following code:
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(); }
The Contains method allows for flexible matching in the IN clause, enabling developers to specify a collection of values against which to compare the specified attribute in the Entity Framework query.
The above is the detailed content of How Can I Use Attributes with IN Clause Queries in Entity Framework?. For more information, please follow other related articles on the PHP Chinese website!