Ordering by the same property in twice, once in each directions, throws

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • None
    • None
    • Dotnet Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      This is not a valid query for people to write, but it can happen if the user supplies an ordering, and then EF also applies an ordering.

      Repro:

      using var context = new SomeDbContext();
      context.Database.EnsureDeleted();
      context.Database.EnsureCreated();
      
      context.AddRange(
          new Order { Id = 1, Foo = 1 },
          new Order { Id = 2, Foo = 1 },
          new Order { Id = 3, Foo = 1 },
          new Order { Id = 4, Foo = 3 },
          new Order { Id = 5, Foo = 3 },
          new Order { Id = 6, Foo = 3 });
      
      context.SaveChanges();
      
      var results = context.Set<Order>().OrderBy(o => o.Foo).ThenByDescending(o => o.Foo).ToList();
      foreach (var result in results)
      {
          Console.WriteLine($"{result.Id} {result.Foo}");
      }
      
      public class SomeDbContext : DbContext
      {
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
              => optionsBuilder
                  //.UseSqlite("DataSource = db.dat")
                  .UseMongoDB("mongodb://localhost:27017", "SomeDb3")
                  .LogTo(Console.WriteLine, LogLevel.Information);
      
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity<Order>();
          }
      }
      
      public class Order
      {
          public int Id { get; set; }
          public int Foo { get; set; }
      }
      
      Unhandled exception. System.InvalidOperationException: Duplicate element name 'Foo'.
         at MongoDB.Bson.BsonDocument.Add(BsonElement element)
         at MongoDB.Bson.BsonDocument.AddRange(IEnumerable`1 elements)
         at MongoDB.Bson.BsonDocument..ctor(IEnumerable`1 elements)
         at MongoDB.Driver.Linq.Linq3Implementation.Ast.AstSortFields.Render()
         at MongoDB.Driver.Linq.Linq3Implementation.Ast.Stages.AstSortStage.Render()
         at MongoDB.Driver.Linq.Linq3Implementation.Ast.AstPipeline.<>c.<Render>b__10_0(AstStage s)
         at System.Linq.Enumerable.IListSelectIterator`2.MoveNext()
         at MongoDB.Bson.BsonArray.AddRange(IEnumerable`1 values)
         at MongoDB.Bson.BsonArray..ctor(IEnumerable`1 values)
         at MongoDB.Driver.Linq.Linq3Implementation.Ast.AstPipeline.Render()
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExecutableQuery`3.RenderPipeline()
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExecutableQuery`3.Execute(IClientSessionHandle session, CancellationToken cancellationToken)
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQueryProvider`1.Execute[TResult](ExecutableQuery`2 executableQuery, CancellationToken cancellationToken)
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.Execute()
         at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.GetEnumerator()
         at MongoDB.EntityFrameworkCore.Query.QueryingEnumerable`2.Enumerator.MoveNextHelper()
         at MongoDB.EntityFrameworkCore.Query.QueryingEnumerable`2.Enumerator.MoveNext()
         at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
         at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
         at Program.<Main>$(String[] args) in /Users/arthur.vickers/code/OnYourOwnNow/OnYourOwnNow/Program.cs:line 20
      

            Assignee:
            Unassigned
            Reporter:
            Arthur Vickers
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: