Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ in Entity Framework 6 with large .Any()

I have a EF6 query that takes a list of IDs and does a query:

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 ids, but when that gets to hundreds I get the error:

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

How do I get the query to return only then the ids passed in? I can not change the data base :(

like image 572
Ian Vink Avatar asked Feb 14 '23 20:02

Ian Vink


1 Answers

Use Contains instead:

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

Which should be transformed into IN within generated SQL query.

like image 101
MarcinJuraszek Avatar answered Feb 24 '23 16:02

MarcinJuraszek