/// <summary> /// The sort. /// </summary> /// <param name="entities"> /// The entities. /// </param> /// <returns> /// The <see cref="IQueryable" />. /// </returns> public IQueryable <TEntity> Sort(IQueryable <TEntity> entities) { IOrderedQueryable <TEntity> orderedEntities = null; for (int i = 0; i < _param.SortingColumnsCount; i++) { int sortingColumn = _param.SortingColumns[i]; string sortingDirection = _param.SortDirections[i]; IDatatableProperty <TEntity> property = _properties[sortingColumn]; Type keyType = property.SortBy.ReturnType; var typeArguments = new[] { typeof(TEntity), keyType }; IDatatablePropertySorter <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); }
/// <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> > >(); for (int i = 0; i < _param.Search.Length; i++) { string search = _param.Search[i]; if (_param.Searchable[i] && !string.IsNullOrEmpty(search)) { IDatatableProperty <TEntity> property = _properties[i]; /** * 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.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.Expand())); } return(entities); }
public void SetUp() { _param = new DatatableParam { ColumnsCount = 2, DataProperties = new[] {"Id", "Name"}, DatatableId = "PeopleDatatable", DisplayLength = 10, DisplayStart = 0, Echo = "abc", GlobalRegex = false, GlobalSearch = "", Regex = new[] {false, false}, Search = new string[] {null, null}, Searchable = new[] {false, true}, }; _properties = new IDatatableProperty<Person>[] { new DatatableProperty<Person, string>("Id", person => person.Id), new DatatableProperty<Person, int>("Name", person => person.Name), new DatatableProperty<Person, DateTime>("Birthday", person => person.Birthday, "yyyy-MM-dd") }; }
public void SetUp() { _param = new DatatableParam { ColumnsCount = 2, DataProperties = new[] { "Id", "Name" }, DatatableId = "PeopleDatatable", DisplayLength = 10, DisplayStart = 0, Echo = "abc", GlobalRegex = false, GlobalSearch = "", Regex = new[] { false, false }, Search = new string[] { null, null }, Searchable = new[] { false, true }, }; _properties = new IDatatableProperty <Person>[] { new DatatableProperty <Person, string>("Id", person => person.Id), new DatatableProperty <Person, int>("Name", person => person.Name), new DatatableProperty <Person, DateTime>("Birthday", person => person.Birthday, "yyyy-MM-dd") }; }