dotnet / dotnet/efcore

Where_compare_constructed_multi_value_not_equal: anonymous object equality should use value semantics

Open
#36,412 3 comments 0 reactions 0 assignees View on GitHub
area-test
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Query is:

```C#
ss.Set().Where(c => new { x = c.City, y = c.Country } != new { x = "London", y = "UK" })
```

The MongoDB provider supports this and returns correct results. For example:

```C#
context.AddRange(
new Customer { CustomerID = "1", Country = "UK", City = "abc" },
new Customer { CustomerID = "2", Country = "USA", City = "London" },
new Customer { CustomerID = "101", Country = "UK", City = "London" },
new Customer { CustomerID = "102", Country = "UK", City = "jkl" });

context.SaveChanges();

var results = context.Set()
.Where(c => new { x = c.City, y = c.Country } != new { x = "London", y = "UK" })
.ToList();

foreach (var result in results)
{
Console.WriteLine($"Found {result} {result.City} { result.Country}");
}

```

Prints all results that are not London, UK:

```
info: 22/07/2025 13:23:16.815 MongoEventId.ExecutedMqlQuery[35000] (Microsoft.EntityFrameworkCore.Database.Command)
Executed MQL query
SomeDb5.Customers.aggregate([{ "$match" : { "$expr" : { "$ne" : [{ "x" : "$City", "y" : "$Country" }, { "x" : "London", "y" : "UK" }] } } }])
Found Customer abc UK
Found Customer London USA
Found Customer jkl UK
```

But Linq to Objects:

```C#
var results = context.Set()
.ToList()
.Where(c => new { x = c.City, y = c.Country } != new { x = "London", y = "UK" })
.ToList();
```

Which the test uses for verification, returns:

```
info: 22/07/2025 13:26:00.213 MongoEventId.ExecutedMqlQuery[35000] (Microsoft.EntityFrameworkCore.Database.Command)
Executed MQL query
SomeDb5.Customers.aggregate([])
Found Customer abc UK
Found Customer London USA
Found Customer London UK
Found Customer jkl UK

```

This includes London, UK, presumably because the object references don't match.

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.