تخطَّ إلى المحتوى

Multiple enumeration (ARCH901)

هذا المحتوى غير متوفر بلغتك بعد.

Key dotnet.linq.multiple-enumeration · Severity warning · Category linq

Iterating the same IEnumerable repeatedly re-executes the underlying query each time.

IEnumerable<Order> orders = GetOrders();
if (orders.Any())
{
var count = orders.Count(); // enumerates again
var first = orders.First(); // and again
}
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.