Order by before where (ARCH904)
Key dotnet.linq.order-by-before-where · Severity warning · Category linq
Sorting before filtering also sorts rows that will be discarded, doing unnecessary work.
Incorrect
Section titled “Incorrect”var result = orders .OrderBy(o => o.Date) .Where(o => o.IsPaid) .ToList();Correct
Section titled “Correct”var result = orders .Where(o => o.IsPaid) .OrderBy(o => o.Date) .ToList();Apply Where() first, then OrderBy().