I'm trying to make sorting easier for an C# Application and C# Web API. I'm using Entity Framework Core for my persistence and testing.
In my application or Web API I determine the Order, Descending or Ascending, Property Name.
I pass this knowledge into my repository where a Linq query is created and executed. The problem is when I have a decimal column its doing a string order rather than a decimal order.
public static class SortingExtensions
{
public static IQueryable<T> SortBy<T>(
this IQueryable<T> queryable,
Sorting sorting)
{
IOrderedQueryable<T> orderedQueryable = null;
sorting.SortableEntities
.OrderBy(x => x.Order)
.ToList()
.ForEach(sortableEntity =>
{
Expression<Func<T, object>> expression = QueryHelper.GetDefaultSortExpression<T>(sortableEntity);
if (expression != null)
{
orderedQueryable = orderedQueryable == null
? queryable.OrderBy(expression, sortableEntity.Descending)
: orderedQueryable.OrderBy(expression, sortableEntity.Descending);
}
});
return orderedQueryable;
}
private static IOrderedQueryable<T> OrderBy<T, TKey>(
this IOrderedQueryable<T> query,
Expression<Func<T, TKey>> keySelector,
bool descending) => descending ? query.ThenByDescending(keySelector) : query.ThenBy(keySelector);
private static IOrderedQueryable<T> OrderBy<T, TKey>(
this IQueryable<T> query,
Expression<Func<T, TKey>> keySelector,
bool descending) => descending ? query.OrderByDescending(keySelector) : query.OrderBy(keySelector);
}
public static class QueryHelper
{
public static Expression<Func<T, object>> GetDefaultSortExpression<T>(SortableEntity sortableEntity)
{
Type entityType = typeof(T);
ParameterExpression arg = Expression.Parameter(entityType, "x");
string[] fieldNames = sortableEntity.Name.Split('.');
MemberExpression memberExpression = null;
foreach (string name in fieldNames)
{
Expression expressionToUse = memberExpression ?? (Expression) arg;
memberExpression = Expression.Property(expressionToUse, name.ToProperCase());
}
Expression propertyExpression = Expression.Convert(memberExpression, typeof(object));
Expression<Func<T, object>>
complexExpression = Expression.Lambda<Func<T, object>>(propertyExpression, arg);
return complexExpression;
}
}
public class SortableEntity
{
public int Order { get; set; }
public bool Descending { get; set; }
public string Name { get; set; }
}
public class Sorting
{
IEnumerable<SortableEntity> SortableEntities { get; }
}
public class TestDecimalPropertyClass : Entity
{
public TestDecimalPropertyClass(decimal @decimal) => Decimal = @decimal;
protected TestDecimalPropertyClass()
{
}
public decimal Decimal { get; set; }
}
public class TestDecimalPropertyClassRepository
{
private readonly DbContext _dbContext;
public TestDecimalPropertyClassRepository(DbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<IEnumerable<TestDecimalPropertyClass>> GetAllAsync(Sorting sorting)
{
List<TestDecimalPropertyClass> entities = await _dbContext.Set<TestDecimalPropertyClass>()
.SortBy(sorting)
.ToListAsync();
return entities;
}
public async Task SaveAsync(TestDecimalPropertyClass testDecimalPropertyClass)
{
_dbContext.Set<TestDecimalPropertyClass>().Add(testDecimalPropertyClass);
await _dbContext.SaveChangesAsync();
}
}
Here is a test I wrote for it:
[TestFixture]
public class GenericSortingTests
{
private SqliteConnection SqliteConnection { get; set; }
[SetUp]
public void DbSetup()
{
SqliteConnectionStringBuilder sqliteConnectionStringBuilder = new SqliteConnectionStringBuilder
{
Mode = SqliteOpenMode.Memory,
Cache = SqliteCacheMode.Private
};
SqliteConnection = new SqliteConnection(sqliteConnectionStringBuilder.ToString());
SqliteConnection.Open();
}
[TearDown]
public void DbTearDown()
{
SqliteConnection.Close();
}
[Test]
public async Task GivenADecimalProperty_WhenISortByColumn_ThenItSorts()
{
decimal[] decimals = new[] {7m, 84.3m, 13.4m};
using (DbContext dbContext = GetDbContext())
{
TestDecimalPropertyClassRepository testRepository = new TestDecimalPropertyClassRepository(dbContext);
foreach (decimal @decimal in decimals)
{
TestDecimalPropertyClass entity = new TestDecimalPropertyClass(@decimal);
await testRepository.SaveAsync(entity);
}
}
IEnumerable<TestDecimalPropertyClass> entities;
using (DbContext dbContext = GetDbContext())
{
TestDecimalPropertyClassRepository testRepository = new TestDecimalPropertyClassRepository(dbContext);
entities = await testRepository.GetAllAsync(new Sorting
{
SortableEntities = new[]
{
new SortableEntity
{
Descending = false,
Name = "decimal",
Order = 0
}
}
});
}
List<TestDecimalPropertyClass> 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<TestDecimalPropertyClass>();
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;
}
}
I expect it to sort the items into the order: 7m, 13.4m, 84.3m but instead it sorts it into 13.4m, 7m, 84.3m
Can anyone help me understand why its doing this so I can fix it?
Thanks, Chris