LINQ in Entity Framework 6 with large .Any()

c# entity-framework entity-framework-6 linq sql

Question

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 :(

1
4
2/9/2014 9:06:28 PM

Accepted Answer

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.

8
2/9/2014 9:05:53 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow