Stephen Holt
Stephen Holt

Reputation: 2368

Combining Entity Framework SqlQuery with extra restriction in code

I have a SQL query

var sql = "Select * From Foo Where Bar = {0}"

I want to execute this using Entity Framework, but I want to impose an extra restriction, to see if column Id is in a certain range:

List<int> ids = ...;
var MyFoos = context.Foos.SqlQuery<Foo>(sql).Where(x => ids.Contains(x.Id));

Is this likely to result in efficient selection from the database, or would it end up executing the whole of "Select * From Foo Where Bar = {0}" first and only then filtering for the IDs?

Upvotes: 1

Views: 108

Answers (1)

Khan
Khan

Reputation: 18162

The SQL statement in sql will be executed database side, and results will be returned to the client.

The filter .Where(x => ids.Contains(x.Id)); will then be executed against the results of your sql query, client side.

The .Where will not be translated to SQL.

I verified this using SQL Profiler on a similar query.

Upvotes: 2

Related Questions