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 #1300

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

Can't translate OrderBy on a generic method #1300

TanielianVB opened this issue Mar 4, 2020 · 3 comments

Comments

@TanielianVB
Copy link

TanielianVB commented Mar 4, 2020

Hi,
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");

        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.

Why?! You were the chosen one! Hahaha

@roji
Copy link
Member

roji commented Mar 4, 2020

Like #1299, this is a generic query translation question which has nothing really to do with Npgsql... Can you please move this to https://github.com/dotnet/efcore? Also, please specify which precise version of EF Core you're using.

@roji roji closed this as completed Mar 4, 2020
@TanielianVB
Copy link
Author

Ok. Thank you.

@TanielianVB
Copy link
Author

Opened dotnet/efcore#20181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants