Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't translate OrderBy on a generic method #20181

Closed
TanielianVB opened this issue Mar 5, 2020 · 3 comments
Closed

Can't translate OrderBy on a generic method #20181

TanielianVB opened this issue Mar 5, 2020 · 3 comments

Comments

@TanielianVB
Copy link
Contributor

Hi,
When ordering by a property defined on a interface witch restricts the generic type of the IQueryable I get the "could not be translated" error.

Steps to reproduce

With the following table:

CREATE TABLE public."Things"
(
    "Guid" bigint NOT NULL,
    CONSTRAINT "Things_pkey" PRIMARY KEY ("Guid")
)

The following sample:

public class EFCoreTestContext : DbContext
{
    public DbSet<Thing> Things { get; set; }

    public static readonly ILoggerFactory DebugLoggerFactory = LoggerFactory.Create(builder => { builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole(); });

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder
        .UseLoggerFactory(DebugLoggerFactory)
        //.UseNpgsql("Host=localhost;Database=EFCoreTest;Username=test;Password=test")
        .UseSqlServer(@"Data Source=(localDB)\MSSQLLocalDB;Initial Catalog=EFCoreTest;Integrated Security=True");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<Thing>(eb =>
            {
                eb.ToTable("Things");
                eb.HasKey(e => e.Guid);
            });
    }
}

public class Thing
{
    public long Guid { get; set; }
}

public interface IDTO
{
    long ID { get; set; }
}

public class DTO : IDTO
{
    public long ID { get; set; }
}

public class ThingDTO : DTO { }

class Program
{
    static IQueryable<TEntity> OrderById<TEntity>(IQueryable<TEntity> query) where TEntity : IEntity => query.OrderBy(e => e.ID);

    static void Main(string[] args)
    {
        using (var db = new EFCoreTestContext())
        {
            var q = (from t in db.Things
                        select new ThingDTO
                        {
                            ID = t.Guid,
                        });

            var r = q.OrderBy(e => e.ID).ToList();

            var r1 = r.ToList();

            Console.WriteLine();

            try
            {
                var r2 = OrderById(q).ToList();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
            }
        }

        Console.ReadKey();
    }
}

I'm getting this output:

--info
--: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT t."Guid" AS "ID"
      FROM "Things" AS t
      ORDER BY t."Guid"
//The LINQ expression 'DbSet<Thing>
    .OrderBy(t => ((IEntity)new ThingDTO{ ID = t.Guid }
    ).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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Further technical details

EF Core version: 3.1.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer or Npgsql.EntityFrameworkCore.PostgreSQL both 3.1.2
Target framework: (e.g. .NET Framework 4.7.2
Operating system: Windows
IDE: Visual Studio 2019 16.4.5

@smitpatel
Copy link
Contributor

Duplicate of #20097

@smitpatel smitpatel marked this as a duplicate of #20097 Mar 16, 2020
@smitpatel smitpatel removed this from the 5.0.0 milestone Mar 16, 2020
@smitpatel smitpatel added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed type-bug labels Mar 16, 2020
@TanielianVB
Copy link
Contributor Author

@smitpatel , thank you.
Is it possible the tell me in which package version the commit 62b88f3 will be in?
Thanks in advance,

@smitpatel
Copy link
Contributor

It is available in daily builds. It will be released as part of 5.0.0 preview3.

@ajcvickers ajcvickers added closed-duplicate and removed area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels Mar 31, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants