Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S6602 FP: "Find" method should not be used for EF Core queries #7964

Closed
pampua84 opened this issue Sep 5, 2023 · 9 comments · Fixed by #8212
Closed

Fix S6602 FP: "Find" method should not be used for EF Core queries #7964

pampua84 opened this issue Sep 5, 2023 · 9 comments · Fixed by #8212
Assignees
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@pampua84
Copy link

pampua84 commented Sep 5, 2023

This type of suggestion when applied to linq queries that are used by EF Core can lead to throwing exceptions of the type:

System.InvalidOperationException: The LINQ expression 'DbSet().... could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

So this thing should at least be specified in the documentation.

@Tim-Pohlmann
Copy link
Contributor

Hi @pampua84, good catch! Thanks for bringing it to our attention.

@Tim-Pohlmann Tim-Pohlmann changed the title Fix S6602 FP/FN: "Find" method should be used instead of the "FirstOrDefault" extension Fix S6602 FP: "Find" method should be used instead of the "FirstOrDefault" extension Sep 6, 2023
@Tim-Pohlmann Tim-Pohlmann changed the title Fix S6602 FP: "Find" method should be used instead of the "FirstOrDefault" extension Fix S6602 FP: "Find" method should not be used for EF Core queries Sep 6, 2023
@Tim-Pohlmann Tim-Pohlmann added Type: False Positive Rule IS triggered when it shouldn't be. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. labels Sep 6, 2023
@Tim-Pohlmann
Copy link
Contributor

Tim-Pohlmann commented Sep 6, 2023

For reference; Similar issues have been resolved for S6605 and S6617: #7286

@costin-zaharia-sonarsource costin-zaharia-sonarsource added the Sprint: Hardening Fix FPs/FNs/improvements label Oct 13, 2023
@sebastien-marichal sebastien-marichal self-assigned this Oct 17, 2023
@sebastien-marichal
Copy link
Contributor

Hello @pampua84,

While the issue has been identified and the fix is in progress, we might miss the case you described.
It would help us a lot if you could provide us with a reproducer illustrating what you described.

Thank you 🙏

@pampua84
Copy link
Author

HI @sebastien-marichal,
what exactly should I do? If I can help you I'll gladly do it.

@sebastien-marichal
Copy link
Contributor

If you can provide me a code snippet of the issue you mentioned: using FirstOrDefault in a linq query used by EF Core where S6602 would raise.

Then, I will be able to ensure this case will be covered.

@pampua84
Copy link
Author

This is the query where the Find() method (on Properties) cannot be applied as suggested by SonarLint:

var person = _repository.GetData<Person>()
    .Include(p => p.Properties)
    .Where(p => p.Id == id && p.Properties.Find(property => property.Value == "IsGoldMember") != null)
    .Select(p => new Person
    {
        Id = p.Id,
        Name = p.Name,
        Description = p.Description,
        Enabled = p.Enabled,
        Created = p.Created,
        Properties = p.Properties
    });

where repository is:

public abstract class RepositoryBase : IRepository
{
    private readonly DbContext _context;  // EF Core DbContext

    protected RepositoryBase(DbContext context)
    {
        Guard.Against.Null(context);

        _context = context;
    }

    //....

    public IQueryable<TEntity> GetData<TEntity>()
        where TEntity : class
    {
        return _context.Set<TEntity>().AsQueryable();
    }

    //.....

}

@pampua84
Copy link
Author

I don't know if only this snippet is good, otherwise if I need a repository with a working application I need more time.

@sebastien-marichal
Copy link
Contributor

Thank you @pampua84!

I am assuming Person.Properties is one of the following types:

  • List<T>
  • Array
  • ImmutableList<T>

If this is the case, I am all good!

@pampua84
Copy link
Author

pampua84 commented Oct 18, 2023

Hi @sebastien-marichal ,
I confirm that the object is a List<T>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants