A deferred LINQ query held in a local is enumerated more than once, so it runs again each time

tx0/csharp:query-enumerated-twicerecommendedwarningperformance/iodepth fastbug, performance

What it finds

A LINQ query such as var active = users.Where(u => u.Active); is not a list but a recipe: every Count(), Any(), ToList() or foreach over active runs it again, from the source, with every lambda in it. For a query on a database that is one round trip per use; for a sequence built by an iterator, it may even give other elements the second time. Materializing the query once with ToList() or ToArray() does the work once.

Reported are locals declared with var, IEnumerable<T>, IQueryable<T> or IOrderedEnumerable<T> whose initializer is a query expression or a call of a deferred operator such as Where or Select with a lambda, when the method enumerates the local twice, or once on every pass of a loop around it. A use in a lambda counts once: in a query on a database such as ids.Contains(x.Id) it is translated into one query. Composing a further query from the local, as in query.Skip(10).Take(10), does not count as an enumeration. Locals that are assigned again, or whose name is declared once more in the method, are left alone.

From the rule's source, query_enumerated_twice.rs.

Example from the rule's tests
using System.Collections.Generic;using System.Linq; class Orders{    int Summary(List<int> amounts, List<int> limits)    {        var large = amounts.Where(a => a > 100);        if (large.Any())        {            return large.Count();tx0 reports: the query large is enumerated again here, and runs again        }        IEnumerable<int> sorted = from a in amounts orderby a select a;        int total = 0;        foreach (int limit in limits)        {            total += sorted.Count();tx0 reports: the query sorted is run again on every pass of the loop        }        var odd = amounts.Where(a => a % 2 == 1);        bool hit = limits.Any(l => odd.Contains(l));        return total;    }     List<int> Page(List<int> amounts, int skip)    {        var query = amounts.Where(a => a > 0);        int count = query.Count();        var page = query.Skip(skip).Take(10).ToList();        var list = amounts.Where(a => a > 1).ToList();        int first = list.Count + list.Count();        var once = amounts.Select(a => a * 2);        foreach (int item in once)        {            count += item;        }        var again = amounts.Where(a => a > 2);        again = again.ToList();        int n = again.Count() + again.Count();        var buffer = amounts.Skip(1);        int m = buffer.Count() + buffer.Count();        return page;    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:query-enumerated-twice" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:query-enumerated-twice] reason="..."

tx0 explain tx0/csharp:query-enumerated-twice shows the same in the terminal. Found a false positive? Report it.