Querying Entity Model. Part 5 – Implementing WHERE IN Analogue

Working with SQL databases, you could encounter some cases where you write something like:

SELECT ...
FROM ...
WHERE ... IN (subquery)

This type of queries can be implemented in Entity Framework model. We need to place a subquery in Where clause.
My example uses the same Client and Phone entities from the first post about Querying Entity Model. The LINQ query is quite simple, and it has lambda syntax.

T-SQL query generated by Entity Framework uses EXISTS command instead of WHERE … IN. But the result is the same comparing with the alternative query.

If you want to get a WHERE … NOT IN query, place negation operator ! in Where clause right before the subquery:

[UPDATE 2016-08-31]
You can use another form of writing the subquery evaluation with Any() method.

Entity Framework generates the same T-SQL query.

Leave a Reply

Your email address will not be published. Required fields are marked *