Exemplo n.º 1
0
        /// <summary>
        /// Parses the <see cref="HttpRequestBase"/> parameter values for the accepted
        /// DataTable request values
        /// </summary>
        /// <returns>Formated output for DataTables, which should be serialized to JSON</returns>

        public FormatedList <T> Parse()
        {
            var list = new FormatedList <T>();

            // parse the echo property (must be returned as int to prevent XSS-attack)
            list.draw = int.Parse(_httpRequest[Constants.DRAW]);

            // count the record BEFORE filtering
            list.recordsTotal = _queriable.Count();

            // apply the sort, if there is one
            ApplySort();

            // parse the paging values
            int skip = 0, take = 10;

            int.TryParse(_httpRequest[Constants.DISPLAY_START], out skip);
            int.TryParse(_httpRequest[Constants.DISPLAY_LENGTH], out take);

            //This needs to be an expression or else it won't limit results
            Func <T, bool> GenericFind = delegate(T item)
            {
                bool found   = false;
                var  sSearch = _httpRequest[Constants.SEARCH_KEY];

                if (string.IsNullOrWhiteSpace(sSearch))
                {
                    return(true);
                }

                foreach (var map in _propertyMap)
                {
                    if (map.Value.Searchable && Convert.ToString(map.Value.Property.GetValue(item, null)).ToLower().Contains((sSearch).ToLower()))
                    {
                        found = true;
                    }
                }
                return(found);
            };

            //Test for linq to entities
            //Anyone know of a better way to do this test??
            if (_queriable is ObjectQuery <T> || _queriable is DbQuery <T> )
            {
                // setup the data with individual property search, all fields search,
                // paging, and property list selection
                var resultQuery = _queriable.Where(ApplyGenericSearch)
                                  .Skip(skip)
                                  .Take(take);

                list.data = resultQuery.ToList();

                list.SetQuery(resultQuery.ToString());

                // total records that are displayed after filter
                list.recordsFiltered = string.IsNullOrWhiteSpace(_httpRequest[Constants.SEARCH_KEY]) ? list.recordsTotal : _queriable.Count(ApplyGenericSearch);
            }
            else //linq to objects
            {
                // setup the data with individual property search, all fields search,
                // paging, and property list selection
                var resultQuery = _queriable.Where(GenericFind)
                                  .Skip(skip)
                                  .Take(take);

                list.data = resultQuery
                            .ToList();

                list.SetQuery(resultQuery.ToString());


                // total records that are displayed after filter
                list.recordsFiltered = string.IsNullOrWhiteSpace(_httpRequest[Constants.SEARCH_KEY])? list.recordsTotal : _queriable.Count(GenericFind);
            }



            return(list);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses the <see cref="HttpRequestBase"/> parameter values for the accepted
        /// DataTable request values
        /// </summary>
        /// <returns>Formated output for DataTables, which should be serialized to JSON</returns>

        public FormatedList <T> Parse()
        {
            var list = new FormatedList <T>();

            // import property names
            list.Import(_properties.Select(x => x.Name).ToArray());

            // parse the echo property (must be returned as int to prevent XSS-attack)
            list.sEcho = int.Parse(_httpRequest[ECHO]);

            // count the record BEFORE filtering
            list.iTotalRecords = _queriable.Count();

            // apply the sort, if there is one
            ApplySort();

            // parse the paging values
            int skip = 0, take = 10;

            int.TryParse(_httpRequest[DISPLAY_START], out skip);
            int.TryParse(_httpRequest[DISPLAY_LENGTH], out take);

            //This needs to be an expression or else it won't limit results
            Func <T, bool> GenericFind = delegate(T item)
            {
                bool bFound  = false;
                var  sSearch = _httpRequest["sSearch"];

                if (string.IsNullOrWhiteSpace(sSearch))
                {
                    return(true);
                }

                foreach (PropertyInfo property in _properties)
                {
                    if (Convert.ToString(property.GetValue(item, null)).ToLower().Contains((sSearch).ToLower()))
                    {
                        bFound = true;
                    }
                }
                return(bFound);
            };

            //Test ofr linq to entities
            //Anyone know of a better way to do this test??
            if (_queriable is ObjectQuery <T> || _queriable is DbQuery <T> )
            {
                // setup the data with individual property search, all fields search,
                // paging, and property list selection
                var resultQuery = _queriable.Where(ApplyGenericSearch)
                                  .Skip(skip)
                                  .Take(take);

                list.aaData = resultQuery.ToList();

                list.SetQuery(resultQuery.ToString());

                // total records that are displayed after filter
                list.iTotalDisplayRecords = _queriable.Count(ApplyGenericSearch);
            }
            else //linq to objects
            {
                // setup the data with individual property search, all fields search,
                // paging, and property list selection
                var resultQuery = _queriable.Where(GenericFind)
                                  .Skip(skip)
                                  .Take(take);

                list.aaData = resultQuery
                              .ToList();

                list.SetQuery(resultQuery.ToString());

                // total records that are displayed after filter
                list.iTotalDisplayRecords = _queriable.Count(GenericFind);
            }



            return(list);
        }