I am trying to implement OrderBy
and ThenBy
in a different way to hide lambda expression from OrderBy
and ThenBy
extension methods. These extension methods accept classes which implement IOrderSpecification
:
public class PersonOrderByAgeSpecification : OrderSpecification<Person>
{
public PersonOrderByAgeSpecification(Sort direction= Sort.Ascending) : base(direction)
{
}
public override Expression<Func<Person, IComparable>> AsExpression()
{
return personOrder => personOrder.Age;
}
}
And the usage:
var orderSpecification = new PersonOrderByAgeSpecification(Sort.Ascending);
var sortedPeople= _dbContext.People.OrderBy(orderSpecification);
It works fine when the property type in AsExpression()
is just string. For example:
public override Expression<Func<Person, IComparable>> AsExpression()
{
return personOrder => personOrder.FirstName;
}
Otherwise I would get this error: (Does not work with integer or bool)
InvalidOperationException: Null TypeMapping in Sql Tree Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalSqlTranslatingExpressionVisitor+SqlTypeMappingVerifyingExpressionVisitor.VisitExtension(Expression node)
The source code is available here
I appreciate any help.
List<Person>
I made up, againststring
,int
andbool
sortings all of which worked just fine. It seems like you are hitting an issue with EFCore's RelationalSqlTranslatingExpressionVisitor, which can be found here: github.com/aspnet/EntityFrameworkCore/blob/… You might want to open a new issue.