I am trying to run a query that normally runs perfectly but if the list is too big, it gets a timeout error.
This is my code:
public async Task<IEnumerable<User>> GetUsers(IEnumerable<int> ids)
{
if (ids.IsNullOrEmpty())
{
return Enumerable.Empty<User>();
}
string query = $@" SELECT *
FROM dbo.Users
WHERE Id IN ({string.Join(",", ids)})";
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
return await conn.QueryAsync<TEntity>(query);
}
}
If the ids count is small (less then about 100K) it works fine. But if the count is about 1 Million it causes to a SQL Timeout exception.
When I look at the SQL profiler, I see that the query even didn't run.
What am I doing wrong? What would be a good solution or a good way to deal with huge amounts of data in C# => SQL Server queries?
Thanks in advance.