dotnet / dotnet/efcore

ToHashSet not being translated when using OData

Open
#32,090 6 comments 0 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

EF Core version: 7 and 8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0

---------

When using a simple query like:

```
// object model type
public class MyType
{
public Guid Id { get; set; }
HashSet SomeHashSet { get; set; } = new();
}

// db models
public class DbMyType
{
public Guid Id { get; set; }
ICollection Overrides { get; set; }
}

public class DbOtherType
{
public Guid Id { get; set; }
}
```

```
[HttpGet]
[ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
[EnableQuery]
public IQueryable GetAll()
{
return _applicationDbContext.Set()
.Select(x => new MyType
{
Id = x.Id,
SomeHashSet = x.Overrides.Select(y => y.Id).ToHashSet()
});
}
```

This works fine calling the GetAll api until you start to do an OData query like api/MyType?$select=SomeHashSet.

This results in an error when translating to SQL even though the ToHashSet should have no impact on the SQL.

If the SomeHashSet property is a List instead of a HashSet, then it works fine.

Can IEnumerable.ToHashSet() be handled the same way as ToList as the SQL side really shouldn't be getting affected by it in the call chain, only the in memory side should care about the ToHashSet to be hydrating the response object.

If there is a way to override this then could you point me to how to do this, I know you can have some function overrides like the EF.Functions but these are more for scalar functions (I think) than how this should work.

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.