JetBrains Rider 2026.1 Help

Code inspection: Replace with OfType<T>().Last() (replace with OfType<T>().Last(..))

This inspection reports a LINQ query that casts elements with as, filters out null, and then looks for the last element matching an additional condition. The same query is easier to read with OfType<T>().Last(...).

Example

var last = items.Select(x => x as Person).Last(y => y != null && y.IsActive);
var last = items.OfType<Person>().Last(y => y.IsActive);

Quick-fix

Replace the Select(... as T).Last(y => y != null && ...) pattern with OfType<T>().Last(...).

29 March 2026