diff --git a/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs b/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs index c5dfcd9..a5cbeb1 100644 --- a/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs +++ b/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs @@ -92,11 +92,23 @@ protected static BinaryExpression MakeComparisonExpression( else { return compare( - memberAccess, - EnsureMatchingType(memberAccess, referenceValue)); + EnsureAdditionalConversions(memberAccess), + EnsureAdditionalConversions(EnsureMatchingType(memberAccess, referenceValue))); } } + private static Expression EnsureAdditionalConversions(Expression expression) + { + if (expression.Type.IsEnum) + { + var enumUnderlyingType = Enum.GetUnderlyingType(expression.Type); + + return Expression.Convert(expression, enumUnderlyingType); + } + + return expression; + } + protected static Expression EnsureMatchingType( Expression memberExpression, Expression targetExpression) diff --git a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs index 27b614c..252e355 100644 --- a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs +++ b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs @@ -77,8 +77,10 @@ private void Seed(TestDbContext context) Inner = new NestedInnerModel { Created = created, + NestedEnumValue = i % 2 == 0 ? TestEnum.Value1 : TestEnum.Value2, }, Inners2 = Enumerable.Range(0, rand.Next(10)).Select(i => new NestedInner2Model()).ToList(), + EnumValue = i % 2 == 0 ? TestEnum.Value1 : TestEnum.Value2, }); } diff --git a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs index 68154b1..1b8913c 100644 --- a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs +++ b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs @@ -25,6 +25,8 @@ public enum QueryType NullCoalescing, NullCoalescing2, Count, + Enum, + NestedEnum, } private const int Size = 10; @@ -58,6 +60,8 @@ public KeysetPaginationTest(DatabaseFixture fixture) QueryType.NullCoalescing => q => q.OrderBy(x => x.CreatedNullable ?? DateTime.MinValue).ThenBy(x => x.Id), QueryType.NullCoalescing2 => q => q.OrderBy(x => x.CreatedNullable ?? x.Created).ThenBy(x => x.Id), QueryType.Count => q => q.OrderBy(x => x.Inners2.Count).ThenBy(x => x.Id), + QueryType.Enum => q => q.OrderBy(x => x.EnumValue).ThenBy(x => x.Id), + QueryType.NestedEnum => q => q.OrderBy(x => x.Inner.NestedEnumValue).ThenBy(x => x.Id), _ => throw new NotImplementedException(), }; Action> keysetPaginationBuilder = queryType switch @@ -75,6 +79,8 @@ public KeysetPaginationTest(DatabaseFixture fixture) QueryType.NullCoalescing => b => b.Ascending(x => x.CreatedNullable ?? DateTime.MinValue).Ascending(x => x.Id), QueryType.NullCoalescing2 => b => b.Ascending(x => x.CreatedNullable ?? x.Created).Ascending(x => x.Id), QueryType.Count => b => b.Ascending(x => x.Inners2.Count).Ascending(x => x.Id), + QueryType.Enum => q => q.Ascending(x => x.EnumValue).Ascending(x => x.Id), + QueryType.NestedEnum => q => q.Ascending(x => x.Inner.NestedEnumValue).Ascending(x => x.Id), _ => throw new NotImplementedException(), }; diff --git a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs index 558d97c..4c12677 100644 --- a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs +++ b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs @@ -23,6 +23,8 @@ public class MainModel public DateTime CreatedComputed { get; } + public TestEnum EnumValue { get; set; } + public NestedInnerModel Inner { get; set; } public List Inners2 { get; set; } @@ -34,6 +36,8 @@ public class NestedInnerModel public int Id { get; set; } public DateTime Created { get; set; } + + public TestEnum NestedEnumValue { get; set; } } public class NestedInner2Model @@ -44,3 +48,9 @@ public class NestedInner2Model public MainModel MainModel { get; set; } } + +public enum TestEnum +{ + Value1, + Value2, +}