C#idiomatic C#
ide0121fundamentals/code-analysis/style-rules

Code analysis

Simplify LINQ type check and cast (IDE0121)

Learn about code analysis rule IDE0121: Simplify LINQ type check and cast

IDE0121 Style / Unnecessary code rules (expression-level preferences)
Applicable languages C# and Visual Basic

Overview

This rule flags LINQ expressions where the type of elements of a sequence are checked (by calling System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable{},System.Func{,System.Boolean})](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.where--1)) and then cast to that type (by calling either [System.Linq.Enumerable.Cast(System.Collections.IEnumerable) or System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable{},System.Func{,})). The code fixer converts these expressions to call [System.Linq.Enumerable.OfType`(System.Collections.IEnumerable)](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.oftype--1) instead.

Options

This rule has no associated code-style options.

Example

0

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

1

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

2

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

ini
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

For more information, see How to suppress code analysis warnings.

See also