CodeQL documentation

Redundant Select

ID: cs/linq/useless-select
Kind: problem
Severity: warning
Precision: very-high
Tags:
   - maintainability
   - language-features
   - external/cwe/cwe-561
Query suites:
   - csharp-security-and-quality.qls

Click to see the query in the CodeQL repository

Passing an identity function to LINQ’s Select method (either explicitly or implicitly) yields a sequence that is the same as the one on which Select was called - such a call is redundant.

Recommendation

Remove the redundant select method call.

Example

In this example the call to the Select method has no effect and can be removed.

class RedundantSelect
{
    static void Main(string[] args)
    {
        List<int> lst = Enumerable.Range(1, 10).ToList();

        foreach (int i in lst.Select(e => e).Where(e => e % 2 == 0))
        {
            Console.WriteLine(i);
        }
    }
}