dotnet / dotnet/efcore

Consider allowing instantiation with non-parameterless constructor in queries, treating it the same as initializers

Open
#34,562 9 comments 2 reactions 0 assignees View on GitHub
area-query customer-reported needs-design
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

## Ask a question

I need to construct a query that does projection (Select) first and then applies OrderBy/Where/Skip/Take etc. on the projected entity. It works when the projected type uses standard parameterless constructor and normal properties.

However when I try to use c# record for projected entity with single primary construtor it fails because it can't translate the OrderBy clause into SQL. I wonder if that is known limitation because I haven't found anything mentioned about it.

### Include your code

```
public class MyEntity {
public int Id { get; set; }
public string Name { get; set; }
}

public record MyEntityDto(string Name);

var result = context.Set().Select(e => new MyEntityDto(e.Name)).OrderBy(dto => dto.Name).ToList();
```

### Include stack traces

```
System.InvalidOperationException: The LINQ expression 'DbSet()
.OrderBy(t => new TestTeam(t.Id).Id)' 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.
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ___REDACTED___DevExtremeQueryBuilder`2.ExecuteAsync(CancellationToken cancellationToken) in ___REDACTED___\DevExtremeQueryBuilder.cs:line 25
```

### Include provider and version information

EF Core version: 8.0.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8
Operating system: Win11
IDE: Rider 2024.2.2

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.