Open hakenr opened 6 days ago
@jirikanda commented in #865:
HxGridColumn
has a propertySortKeySelector
of typeExpression<Func<TItem, IComparable>>
. When you assign a value, ie.employee => employee.Name
, it is converted toemployee => (IComparable)employee.Name
. But this expression cannot be translated to a EF Core query (I have already solved this elsewhere) as mentioned in the exception above.As a solution for EF Core the
Convert
part of expression must be removed. It can be done in theApplyGridDataProviderRequest
method or in theHxGridColumn
. I am not sure what fits you best.The change in
ApplyGridDataProviderRequest
works for sure and I am not sure if there can be some side effect when you change theHxGridColumn
.The solution for the
ApplyGridDataProviderRequest
method can be done by adding a local functionRemoveConvertFromSortKeySelector
with the usage below.public static IQueryable<TItem> ApplyGridDataProviderRequest<TItem>(this IQueryable<TItem> source, GridDataProviderRequest<TItem> gridDataProviderRequest) { Expression<Func<TItem, IComparable>> RemoveConvertFromSortKeySelector(Expression<Func<TItem, IComparable>> sortKeySelectorExpressionLambda) => Expression.Lambda<Func<TItem, IComparable>>(sortKeySelectorExpressionLambda.Body.RemoveConvert(), sortKeySelectorExpressionLambda.Parameters[0]); gridDataProviderRequest.CancellationToken.ThrowIfCancellationRequested(); // Sorting if ((gridDataProviderRequest.Sorting != null) && gridDataProviderRequest.Sorting.Any()) { Contract.Assert(gridDataProviderRequest.Sorting.All(item => item.SortKeySelector != null), $"All sorting items must have the {nameof(SortingItem<TItem>.SortKeySelector)} property set."); IOrderedQueryable<TItem> orderedDataProvider = (gridDataProviderRequest.Sorting[0].SortDirection == SortDirection.Ascending) ? source.OrderBy(RemoveConvertFromSortKeySelector(gridDataProviderRequest.Sorting[0].SortKeySelector)) : source.OrderByDescending(RemoveConvertFromSortKeySelector(gridDataProviderRequest.Sorting[0].SortKeySelector)); for (int i = 1; i < gridDataProviderRequest.Sorting.Count; i++) { orderedDataProvider = (gridDataProviderRequest.Sorting[i].SortDirection == SortDirection.Ascending) ? orderedDataProvider.ThenBy(RemoveConvertFromSortKeySelector(gridDataProviderRequest.Sorting[i].SortKeySelector)) : orderedDataProvider.ThenByDescending(RemoveConvertFromSortKeySelector(gridDataProviderRequest.Sorting[i].SortKeySelector)); } source = orderedDataProvider; } // Paging / Infinite scroll if (gridDataProviderRequest.StartIndex > 0) { source = source.Skip(gridDataProviderRequest.StartIndex); } if (gridDataProviderRequest.Count > 0) { source = source.Take(gridDataProviderRequest.Count.Value); } return source; } }
Discussed in https://github.com/havit/Havit.Blazor/discussions/865
Confirmed by @jirikanda