internal static Expression CompileInitialSortOperation( this Expression source, SortOperationInvocation operation, ParameterExpression parameter) { Expression lambda = HandleProperty(operation, parameter); Type type = typeof(Enumerable); if (typeof(IOrderedQueryable).IsAssignableFrom(source.Type) || typeof(IQueryable).IsAssignableFrom(source.Type)) { type = typeof(Queryable); } if (operation.Kind == SortOperationKind.Desc) { return(Expression.Call( type, "OrderByDescending", new[] { parameter.Type, operation.Property.PropertyType }, source, lambda)); } return(Expression.Call( type, "OrderBy", new[] { parameter.Type, operation.Property.PropertyType }, source, lambda)); }
internal static Expression CompileInitialSortOperation( this Expression source, SortOperationInvocation operation) { Expression lambda = operation.CreateProperty(); Type type = typeof(Enumerable); if (typeof(IOrderedQueryable).IsAssignableFrom(source.Type) || typeof(IQueryable).IsAssignableFrom(source.Type)) { type = typeof(Queryable); } if (operation.Kind == SortOperationKind.Desc) { return(Expression.Call( type, nameof(Queryable.OrderByDescending), new[] { operation.Parameter.Type, operation.ReturnType }, source, lambda)); } return(Expression.Call( type, nameof(Queryable.OrderBy), new[] { operation.Parameter.Type, operation.ReturnType }, source, lambda)); }
internal static Expression CreateProperty( this SortOperationInvocation operation) { return(Expression.Lambda( operation.ExpressionBody, operation.Parameter )); }
internal static Expression HandleProperty( SortOperationInvocation operation, ParameterExpression parameter) { PropertyInfo propertyInfo = operation.Property; MemberExpression property = Expression.Property(parameter, propertyInfo); return(Expression.Lambda(property, parameter)); }
internal static Expression <Func <TSource, object> > HandleProperty <TSource>( SortOperationInvocation operation, ParameterExpression parameter) { PropertyInfo propertyInfo = operation.Property; MemberExpression property = Expression.Property(parameter, propertyInfo); UnaryExpression propAsObject = Expression.Convert(property, typeof(object)); return(Expression.Lambda <Func <TSource, object> >(propAsObject, parameter)); }
internal static Expression <Func <TSource, object> > HandleProperty <TSource>( SortOperationInvocation operation) { UnaryExpression propAsObject = Expression.Convert( operation.ExpressionBody, typeof(object) ); return(Expression.Lambda <Func <TSource, object> >( propAsObject, operation.Parameter )); }
internal static IOrderedQueryable <TSource> AddInitialSortOperation <TSource>( this IQueryable <TSource> source, SortOperationInvocation operation) { Expression <Func <TSource, object> > lambda = HandleProperty <TSource>(operation); if (operation.Kind == SortOperationKind.Desc) { return(source.OrderByDescending(lambda)); } return(source.OrderBy(lambda)); }
internal static IOrderedQueryable <TSource> AddSortOperation <TSource>( this IOrderedQueryable <TSource> source, SortOperationInvocation operation, ParameterExpression parameter) { Expression <Func <TSource, object> > lambda = HandleProperty <TSource>(operation, parameter); if (operation.Kind == SortOperationKind.Desc) { return(source.ThenByDescending(lambda)); } return(source.ThenBy(lambda)); }
public void AddInitialSortOperation_AscOnIQueryable_ShouldAddOrderBy() { // arrange IQueryable <Foo> source = new Foo[0].AsQueryable(); var operation = new SortOperationInvocation( SortOperationKind.Asc, typeof(Foo).GetProperty(nameof(Foo.Bar))); ParameterExpression parameter = Expression.Parameter(typeof(Foo)); // act IOrderedQueryable <Foo> sorted = source.AddInitialSortOperation( operation, parameter ); // assert Assert.Equal(source.OrderBy(s => s.Bar), sorted); }
public void CompileInitialSortOperation_AscOnIQueryable_ShouldAddOrderBy() { // arrange IQueryable <Foo> source = new Foo[0].AsQueryable(); var closure = new SortQueryableClosure(typeof(Foo), "p"); closure.EnqueueProperty(typeof(Foo).GetProperty(nameof(Foo.Bar))); SortOperationInvocation operation = closure.CreateSortOperation(SortOperationKind.Asc); // act Expression sortExpression = source.Expression.CompileInitialSortOperation( operation ); IQueryable <Foo> sorted = source.Provider.CreateQuery <Foo>(sortExpression); // assert Assert.Equal(source.OrderBy(s => s.Bar), sorted); }
public void CompileSortOperation_DescOnIOrderedQueryable_ShouldAddThenByDescending() { // arrange IOrderedQueryable <Foo> source = new Foo[0].AsQueryable().OrderBy(f => f.Bar); var operation = new SortOperationInvocation( SortOperationKind.Desc, typeof(Foo).GetProperty(nameof(Foo.Bar))); ParameterExpression parameter = Expression.Parameter(typeof(Foo)); // act Expression sortExpression = source.Expression.CompileSortOperation( operation, parameter ); IQueryable <Foo> sorted = source.Provider.CreateQuery <Foo>(sortExpression); // assert Assert.Equal(source.ThenByDescending(s => s.Bar), sorted); }