1

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?

1
  • 1
    My expectation would be the latter, because the query is hardcoded rather than something generated by EF itself, and thus there is no way for compilation to improve the query into a single sql read Commented Oct 1, 2013 at 21:40

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll definitely avoid doing that then!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.