Code inspection: Part of loop's body can be converted into LINQ-expression
This inspection reports a foreach loop whose body can only be partly converted into a LINQ-style sequence operation. This warning is produced for foreach loops when part of the body is query-like but the entire loop cannot be replaced with a single LINQ expression.
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 remaining loop logic explicit.
30 March 2026