JetBrains Rider 2026.1 Help

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

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

Example

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

Quick-fix

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

29 March 2026