Exemple #1
0
        public IQueryable <TEntity> Sort(IQueryable <TEntity> entities)
        {
            IOrderedQueryable <TEntity> orderedEntities = null;

            if (_param.order == null)
            {
                return(entities);
            }
            for (int i = 0; i < _param.order.Length; i++)
            {
                var    order                    = _param.order[i];
                int    sortingColumn            = order.column;
                string sortingDirection         = order.dir;
                IDtoProperty <TEntity> property = _properties[sortingColumn];
                Type keyType                    = property.SortBy.ReturnType;

                var typeArguments = new[] { typeof(TEntity), keyType };
                IPagingPropertySorter <TEntity> sorter = MakePropertySorter(typeArguments, property.SortBy);
                if (string.IsNullOrEmpty(sortingDirection) || sortingDirection.Equals(Ascending))
                {
                    orderedEntities = orderedEntities != null
                                          ? sorter.SortAscending(orderedEntities)
                                          : sorter.SortAscending(entities);
                }
                else if (sortingDirection.Equals(Descending))
                {
                    orderedEntities = orderedEntities != null
                                          ? sorter.SortDescending(orderedEntities)
                                          : sorter.SortDescending(entities);
                }
            }

            return(orderedEntities ?? entities);
        }
        public void OnePropertyWeakPropertyTests()
        {
            DtoInfo <OneProperty>      info     = DtoInfo.GetInfo <OneProperty>();
            IDtoProperty <OneProperty> property = info.GetProperty("Integer");

            property.Name.Should().Be("Integer");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeFalse();
            ((PropertyInfo)property.MemberInfo).GetMethod !.Name.Should().Be("get_Integer");

            OneProperty dto = new() { Integer = 42 };

            property.GetValue(dto).Should().Be(dto.Integer);
            property.SetValue(dto, 24);
            dto.Integer.Should().Be(24);
        }
        public void OneFieldWeakestFieldTests()
        {
            DtoInfo <OneField> info     = DtoInfo.GetInfo <OneField>();
            IDtoProperty       property = info.Properties.Single();

            property.Name.Should().Be("Integer");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeFalse();
            ((FieldInfo)property.MemberInfo).Name.Should().Be("Integer");

            OneField dto = new() { Integer = 42 };

            property.GetValue(dto).Should().Be(dto.Integer);
            property.SetValue(dto, 24);
            dto.Integer.Should().Be(24);
        }
Exemple #4
0
        /// <summary>
        ///     Filters the entities
        /// </summary>
        /// <param name="entities">
        ///     The entities.
        /// </param>
        /// <returns>
        ///     The <see cref="IQueryable" />.
        /// </returns>
        public IQueryable <TEntity> Filter(IQueryable <TEntity> entities)
        {
            var expressions = new List <Expression <Func <TEntity, bool> > >();

            if (_param.columns == null)
            {
                return(entities);
            }
            for (int i = 0; i < _param.columns.Length; i++)
            {
                var columns = _param.columns[i];
                var search  = columns.search;
                if (columns.searchable && !string.IsNullOrEmpty(search.value) && search.value != "null")
                {
                    IDtoProperty <TEntity> property = _properties.FirstOrDefault(l => l.ColumnHeader.ToLower() == columns.data.ToLower());

                    /**
                     * Use the ToUpper method to perform a case-ignorant contains operation
                     * I prefer the ToUpper method over its ToLower competitor because Microsoft
                     */
                    MethodCallExpression searchExpr = Expression.Call(
                        Expression.Constant(search.value.Escape()), typeof(string).GetMethod("ToLower", new Type[0]));
                    MethodCallExpression valueToLowerExpression = Expression.Call(
                        property.ToSqlString.Body, typeof(string).GetMethod("ToLower", new Type[0]));
                    MethodCallExpression containsCall = Expression.Call(
                        valueToLowerExpression, typeof(string).GetMethod("Contains"), new Expression[] { searchExpr });
                    LambdaExpression resultCall = Expression.Lambda(containsCall, property.ToSqlString.Parameters);
                    expressions.Add(resultCall as Expression <Func <TEntity, bool> >);
                }
            }

            if (expressions.Count != 0)
            {
                // glue all these expressions into one expression, combined with the AND operator
                Expression <Func <TEntity, bool> > resultLambda = expressions.Aggregate((expr1, expr2) => expr1.And(expr2));
                return(entities.Where(resultLambda));
            }

            return(entities);
        }
Exemple #5
0
 public int?GetPropertyParameterIndex(IDtoProperty <T> property) =>
 m_propertyIndices.TryGetValue(property, out var index) ? index : default(int?);