All rules
IDE0121Unnecessary code rules (expression-level preferences)
Simplify LINQ type check and cast
Simplify LINQ type check and cast
Microsoft docsDescription
This rule flags LINQ expressions where the type of elements of a sequence are checked (by calling System.Linq.Enumerable.Where`1(System.Collections.Generic.IEnumerable{0},System.Func{0,System.Boolean})) and then cast to that type (by calling either System.Linq.Enumerable.Cast1(System.Collections.IEnumerable) or System.Linq.Enumerable.Select2(System.Collections.Generic.IEnumerable{0},System.Func{0,1})). The code fixer converts these expressions to call System.Linq.Enumerable.OfType`1(System.Collections.IEnumerable) instead.
Example
// Code with violations.
IEnumerable<int> y = objects.Where(a => a is int).Cast<int>();
IEnumerable<int> z = objects.Where(a => a is int).Select(a => (int)a);
// Fixed code.
IEnumerable<int> y = objects.OfType<int>();
IEnumerable<int> z = objects.OfType<int>();Your vote
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0