Is there an alternative to using .Contains() to select objects in Entity Framework that exist in a specified list? Contains() works great if your list is small, however once you start getting a few thousands items the performance is terrible.
return (from item in context.Accounts
where accountIdList.Contains(item.AccountId)
select item).ToList();
I’m using EF 4.0, .Net Framework 4.0, and SQL Server 2005. I’m not opposed to a SQL solution either since the query that EF generates only takes a second to run on SQL for about 10k items.
I found an alternative that runs in about a second using a SQL Stored Procedure and a comma-delimited string for the parameter. Much better than the 5+ minutes EF was taking using
.Contains()It is run from my code using the following:
The StoredProcedure (simplified) looks like this:
And the User-Defined function
dbo.StringToNumSet()looks like this: