Linq Implementation of SQL "IN" Statement
In the context of a tagging schema with tables Items, Tags, and TagMap, you seek a LINQ query that retrieves items corresponding to a specified list of tag IDs. In SQL, this is typically achieved using an IN statement.
To translate this into LINQ, you can utilize the Contains() method. Consider the following example:
var TagIds = new int[] {12, 32, 42}; var q = from map in Context.TagMaps where TagIds.Contains(map.TagId) select map.Items;
In this query:
This query will generate an SQL IN statement similar to:
SELECT * FROM Items WHERE TagId IN (12, 32, 42)
The resulting query will return the items that have at least one tag with an ID in the specified list.
The above is the detailed content of How to Implement SQL's 'IN' Statement Using LINQ?. For more information, please follow other related articles on the PHP Chinese website!