Skip to content

NET-3878 Fix S3358 FP: Suggesting to extract ternary from EF Core select #9801

Closed

Description

@grosch-intl

Description

The below code is not able to have the ternary operator extracted as it's part of a linq query, so the whole thing has to be run against each record.

Reproducer

        var query = context.Checklist
            .AsNoTracking()
            .Where(...)
            .Select(x => new {
                ReviewStatus = x.ReviewApprovedUtc.HasValue ? "Approved" : x.ReviewRejectedUtc.HasValue ? "Rejected" : x.ReviewRequestedUtc.HasValue ? "Pending" : null,              

Product and Version

SonarAnalyzer.CSharp 10.23.0.137933

Activity

  1. changed the title [-]Fix S3358 FP: Suggesting to extract ternary from EF Core select[/-] [+]NET-3878 Fix S3358 FP: Suggesting to extract ternary from EF Core select[/+] on Jun 5, 2026
  2. github-actions commented on Jun 5, 2026

    @github-actions
    Contributor

    Internal ticket NET-3878

  3. martin-strecker-sonarsource commented on Jun 5, 2026

    @martin-strecker-sonarsource
    Contributor

    Hi @grosch-intl, thanks for the report — confirmed as a false positive. Inside an IQueryable<T>.Select(...) lambda, the C# compiler converts the body to an Expression<Func<T, TResult>>, where statement-bodied lambdas aren't legal (CS0834) — so the rule's "extract into an independent statement" advice can't be followed.

    Tracked internally as NET-3878 and a fix is up for review. It will roll out with the next analyzer release.

  4. martin-strecker-sonarsource commented on Jun 17, 2026

    @martin-strecker-sonarsource
    Contributor

    The issue was fixed in 7596b50 and will be part of the next release. Thank you @grosch-intl for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    False PositiveRule IS triggered when it shouldn't be.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions