JetBrains Rider 2026.1 Help

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

This inspection reports a LINQ query that casts elements with as and then uses Last(...) to find the last non-null cast result. The same intent is clearer with OfType<T>().Last().

Example

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

Quick-fix

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

29 March 2026