Code inspection: Part of foreach loop can be converted into LINQ-expression but another 'GetEnumerator' method will be used
This inspection reports a foreach loop whose body can only be partly converted into a LINQ-style sequence operation, and doing so will use a different GetEnumerator method than the original loop. This warning is more conservative than the full loop-to-query conversion warning because only part of the loop can be rewritten.
Example
foreach (var item in source)
{
if (item.IsVisible)
yield return item;
Log(item);
}
foreach (var item in source.Where(item => item.IsVisible))
{
yield return item;
Log(item);
}
Quick-fix
Convert the convertible part of the loop body into a query operation while keeping the rest of the loop explicit.
30 March 2026