JetBrains Rider 2026.1 Help

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

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

Example

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

Quick-fix

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

29 March 2026