IQueriable.Contains how to fix the problem when my collection clearly includes an extremely large number of values

Andrej 221 Reputation points
2021-10-20T17:00:57.047+00:00

I have query which me need filtering entries, I use IQueriable.Contains(see ex. below). And when my collection clearly includes an extremely large number of values, I get an error: "Error 8623: The query processor ran out of internal resources and could not produce a query plan.".
Code:

        public async Task<IEnumerable<MyModel>> MyMethod(IEnumerable<int> ids, CancellationToken cancellation)
        {
            //ids.Count > 10000
            var foldersQuery = from folder in _dbSet
                               where  ids.Contains(folder.Id)
                               select folder;

            return await foldersQuery.ToListAsync(cancellation);
        }

It converts to sql query
select * from Table where Id IN (1, ... 99999)

Could you please me fix this issue, or what can replace it.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,205 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 111.8K Reputation points
    2021-10-20T18:55:07.123+00:00

    Did you already find the "WhereBulkContains" extension: https://entityframework-extensions.net/where-bulk-contains?

    1 person found this answer helpful.