dotnet / dotnet/efcore

Avoid executing queries which are known to return no results

Open
#31,405 15 comments 1 reaction 0 assignees View on GitHub
customer-reported
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.