JetBrains Rider 2026.1 Help

Code inspection: Replace with LastOrDefault($args$)

This inspection reports a guarded conditional expression that first checks another condition and then uses Any(...) ? Last(...) : null. When the guard is true, the inner Any/Last pair can be simplified to LastOrDefault(...).

Example

var result = isEnabled && items.Any(x => x.IsValid) ? items.Last(x => x.IsValid) : null;
var result = isEnabled ? items.LastOrDefault(x => x.IsValid) : null;

Quick-fix

Replace the guarded Any(...) ? Last(...) : null pattern with LastOrDefault(...).

29 March 2026