Avoid executing queries which are known to return no results
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
The query in the example is actually meaningless, it should directly return an empty list instead of querying the database
```
using System.ComponentModel.DataAnnotations;
var services = new ServiceCollection();
services.AddDbContext();
var serviceProvider = services.BuildServiceProvider();
var testContext = serviceProvider.GetRequiredService();
var ids = new List();
var test = testContext.Test1.Where(a => ids.Contains(a.Id)).ToList();
public partial class Test1
{
[Key]
public Guid Id { get; set; }
[StringLength(50)]
public string Name { get; set; } = null!;
}
public class TestContext : DbContext
{
public TestContext()
{
}
public TestContext(DbContextOptions options)
: base(options)
{
}
public virtual DbSet Test1 { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
_ = optionsBuilder.UseSqlServer("");
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.