Multiple enumeration (ARCH901)
Esta página aún no está disponible en tu idioma.
Key dotnet.linq.multiple-enumeration · Severity warning · Category linq
Iterating the same IEnumerable repeatedly re-executes the underlying query each time.
Incorrect
Section titled “Incorrect”IEnumerable<Order> orders = GetOrders();if (orders.Any()){ var count = orders.Count(); // enumerates again var first = orders.First(); // and again}Correct
Section titled “Correct”List<Order> orders = GetOrders().ToList();if (orders.Count > 0){ var count = orders.Count; var first = orders[0];}Materialize the source once with ToList() and reuse the list.