You have to first understand how "IN" operator works in parameterized SQL query.
SELECT A FOM B WHERE C IN @p
does not work, SQL command parameter does not accept ARRAY as a parameter value, instead the query is translated to
SELECT A FROM B WHERE C IN (@p1, @p2, @p3 ... etc)
This query has variable number of parameters and this the reason, there is no way to precompile this query with IEnumerable.Contains
.
The only other alternative (long long way) is to use Xml or Json (Coming up in Sql 2016).
Save your IEnumerable as xml.
[10,20,20,50] can be translated to
<data>
<int value="10"/>
<int value="20"/>
<int value="20"/>
<int value="50"/>
</data>
And you can then define a VIEW
with parameters as
SELECT A FROM B WHERE C IN (SELECT INT FROM Xml(@P1))
And you can use this View, however there are more challenges in EF to how to fire this query, but this query can be precompiled as it has only one parameter.
Custom SQL for Performance Hack
For pretty simple query like,
List<Guid> ids;
var entities = context.MyEntities.Where(x => ids.Contains(x.Id)).ToArray();
I could simply use a custom SQL and fire,
var parameterList = ids.Select(
(x,i)=> new SqlCommandParameter(
"@p"+i, x));
var pnames = String.Join(",", parameterList.Select(x=> x.ParameterName));
var entities =
context.SqlQuery<MyEntity>(
"SELECT * FROM TABLE WHERE Id in (" + pnames + ")",
parameterList.ToArray());
Temporary Table
You can also use a temporary table, but this increases number of active transactions in your database.
Guid sid = Guid.NewGuid();
foreach(var p in ids){
db.TempIDs.Add(new TempID{ SID = sid, Value = p });
}
db.SaveChanges();
var qIDs = db.TempIDs.Where( x=> x.SID == sid );
var myEntities db.MyEntities.Where( x => qIDs.Any( q.Value == x.Id) );
// delete all TempIDs...
db.SqlQuery("DELETE FROM TempIDs WHERE SID=@sid,
new SqlCommandParameter("@sid", sid));