简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM