Skip to content

Unable to orderby decimal column #10249

@chris31389

Description

@chris31389

I have a class with a decimal property. I wanted to order all instances of the class by the decimal property within the SQLite database.

I created three instances with these decimal values { 7m, 84.3m, 13.4m }. After running an OrderBy function, I expected it to order it to { 7m, 13.4m, 84.3m } but instead it ordered it to { 13.4m, 7m, 84.3m }

Steps to reproduce

Please run the test below

public class TestEntity : Entity
{
    public TestEntity(decimal @decimal) => Decimal = @decimal;

    protected TestEntity()
    {
    }

    public decimal Decimal { get; set; }
}

[TestFixture]
public class DecimalSortingTests : DbTests
{
    [Test]
    public async Task GivenClassWithDecimalColumn_WhenIOrderBy_ItOrdersCorrectly()
    {
        decimal[] decimals = { 7m, 84.3m, 13.4m };
        IEnumerable<TestEntity> testClasses = decimals.Select(@decimal => new TestEntity(@decimal));
        using (DbContext dbContext = GetDbContext())
        {
            dbContext.Set<TestEntity>().AddRange(testClasses);
            await dbContext.SaveChangesAsync();
        }

        List<TestEntity> entities;
        using (DbContext dbContext = GetDbContext())
        {
            entities = await dbContext.Set<TestEntity>()
                .OrderBy(x => x.Decimal)
                .ToListAsync();
        }

        List<TestEntity> list = entities.ToList();
        Assert.That(list.Count, Is.EqualTo(decimals.Length));
        Assert.That(list.ToArray()[0].Decimal, Is.EqualTo(7m));
        Assert.That(list.ToArray()[1].Decimal, Is.EqualTo(13.4m));
        Assert.That(list.ToArray()[2].Decimal, Is.EqualTo(84.3m));
    }

    private class TestDbContext : DbContext
    {
        public TestDbContext(DbContextOptions options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TestEntity>();
            base.OnModelCreating(modelBuilder);
        }
    }

    private DbContext GetDbContext()
    {
        DbContextOptions<TestDbContext> options = new DbContextOptionsBuilder<TestDbContext>()
            .UseSqlite(SqliteConnection)
            .EnableSensitiveDataLogging()
            .Options;

        TestDbContext dbContext = new TestDbContext(options);
        dbContext.Database.EnsureCreated();
        return dbContext;
    }
}
  Expected: 7m
  But was:  13.4m

Further technical details

EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.Sqlite
Operating system: Windows 10 64x
IDE: Visual Studio 2017

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions