JetBrains Rider 2026.1 Help

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

This inspection reports a LINQ query that casts elements with as and then returns the last non-null cast result or null /default. The same logic is clearer with OfType<T>().LastOrDefault().

Example

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

Quick-fix

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

29 March 2026