简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM