LINQ in Entity Framework 6 with large .Any ()

I have an EF6 request that takes a list of IDs and executes a request:

public IList<Audit> AuditsByIDs(List<int> ids)
{
    return _db.Audits
        .Include(p => p.User)
        .Where(p => ids.Any(i => i == p.Id)).ToList();
}

It works for a small number of identifiers , but when it comes to hundreds, I get an error:

Some part of your SQL statement is nested too deep. Rewrite the query or break it down into smaller queries.

How can I return a request only after the identifiers have passed ? I can not change the database :(

+4
source share
1 answer

Use instead Contains:

public IList<Audit> AuditsByIDs(List<int> ids)
{
    return _db.Audits
        .Include(p => p.User)
        .Where(p => ids.Contains(p.Id)).ToList();
}

IN SQL-.

+8

Source: https://habr.com/ru/post/1526061/


All Articles