JetBrains Rider 2026.1 Help

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

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>().LastOrDefault(...).

Example

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

Quick-fix

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

29 March 2026