示例#1
0
        /// <summary>
        /// Gets table rows based on take count and skip count
        /// </summary>
        /// <param name="takeCount"></param>
        /// <param name="skipCount"></param>
        /// <returns></returns>
        public IEnumerable <T> GetSelectedTableRows(PagingFiltering filters, int takeCount, int skipCount)
        {
            var alllItems = new List <T>();

            skipCount = (skipCount - 1) * 10;
            var fluentQuery = new TableQuery <T>();
            //IEnumerable<T> items = new List<T>();
            //if (filters != null)
            //{
            //    if (null != filters.filter)
            //    {
            //        // TODO: Need expand query filter base on datatype.
            //        if (filters.filter.Filters != null)
            //        {
            //            string filter = "";
            //            List<String> filterCollection = new List<string>();
            //            foreach (var item in filters.filter.Filters)
            //            {
            //                switch (item.Operator)
            //                {
            //                    case "startswith":
            //                        break;
            //                    case "eq":
            //                        filter = filter + TableQuery.GenerateFilterCondition(item.Field, QueryComparisons.Equal, item.Value);
            //                        filterCollection.Add(filter);
            //                        //TableQuery.CombineFilters(filter);
            //                        break;
            //                    case "neq":
            //                        filter = filter + TableQuery.GenerateFilterCondition(item.Field, QueryComparisons.NotEqual, item.Value);
            //                        filterCollection.Add(filter);
            //                        break;
            //                    case "contains":
            //                        break;
            //                    case "endswith":
            //                        break;
            //                }
            //            }
            //            fluentQuery = new TableQuery<T>().Where(filter);
            //        }
            //    }

            //    if (filters.Sort != null)
            //    {
            //        foreach (var sortField in filters.Sort)
            //        {
            //            items = _cloudTable.ExecuteQuery(fluentQuery).OrderBy(m => sortField.Field.GetType()).Skip(skipCount).Take(takeCount);
            //        }
            //    }
            //    else
            //    {
            //        items = _cloudTable.ExecuteQuery(fluentQuery).Skip(skipCount).Take(takeCount);
            //    }
            //}

            TableContinuationToken continuationToken = new TableContinuationToken();
            var items = _cloudTable.ExecuteQuerySegmentedAsync(fluentQuery, continuationToken).Result.Skip(skipCount).Take(takeCount);

            alllItems.AddRange(items);
            return(alllItems);
        }
示例#2
0
        //(PagingFiltering criteria, int pageNo, int pageSize, out long totalCount)
        //public IList<T> SearchFor<T>(string tableName, T entity, PagingFiltering criteria, int pageNo, int pageSize, out long totalCount)
        public TableQuery <T1> SearchFor <T1>(CloudTable table, PagingFiltering criteria, int pageNo, int pageSize, out long totalCount) where T1 : ITableEntity, new()
        {
            var             resultItems = new List <DynamicTableEntity>();
            List <T1>       results     = new List <T1>();
            TableQuery <T1> tableQuery  = new TableQuery <T1>();

            if (null != criteria.filter)
            {
                // TODO: Need expand query filter base on datatype.
                if (criteria.filter.Filters != null)
                {
                    string        filter           = "";
                    List <String> filterCollection = new List <string>();
                    foreach (var item in criteria.filter.Filters)
                    {
                        switch (item.Operator)
                        {
                        case "startswith":
                            break;

                        case "eq":
                            filter = filter + TableQuery.GenerateFilterCondition(item.Field, QueryComparisons.Equal, item.Value);
                            filterCollection.Add(filter);
                            //TableQuery.CombineFilters(filter);
                            break;

                        case "neq":
                            filter = filter + TableQuery.GenerateFilterCondition(item.Field, QueryComparisons.NotEqual, item.Value);
                            filterCollection.Add(filter);
                            break;

                        case "contains":
                            break;

                        case "endswith":
                            break;
                        }
                    }


                    tableQuery = new TableQuery <T1>().Where(filter);



                    //StringBuilder finalFilter = new StringBuilder();
                    //filterCollection.ForEach(delegate (string _filter)
                    //{
                    //    TableQuery.CombineFilters()
                    //});
                }
            }
            totalCount = 0;
            return(tableQuery);
        }
示例#3
0
        public IList <TEntity> SearchFor(PagingFiltering criteria, int pageNo, int pageSize, out long totalCount)
        {
            try
            {
                var resultItems = new List <TEntity>();
                FilterDefinition <TEntity>      filter                = null;
                SortDefinition <TEntity>        sortDefinition        = null;
                SortDefinitionBuilder <TEntity> sortDefinitionBuilder = Builders <TEntity> .Sort;
                int searchval;
                if (null != criteria.filter)
                {
                    // TODO: Need expand query filter base on datatype.
                    if (criteria.filter.Filters != null)
                    {
                        foreach (var item in criteria.filter.Filters)
                        {
                            switch (item.Operator)
                            {
                            case "startswith":


                                if (int.TryParse(item.Value, out searchval))
                                {
                                    if (filter == null)
                                    {
                                        filter = Builders <TEntity> .Filter.Regex(item.Field, new BsonRegularExpression(new Regex("^" + searchval)));
                                    }
                                    else
                                    {
                                        filter = filter & Builders <TEntity> .Filter.Regex(item.Field, new BsonRegularExpression(new Regex("^" + searchval)));
                                    }
                                }
                                else
                                {
                                    if (filter == null)
                                    {
                                        filter = Builders <TEntity> .Filter.Regex(item.Field, new BsonRegularExpression(new Regex("^" + item.Value)));
                                    }
                                    else
                                    {
                                        filter = filter & Builders <TEntity> .Filter.Regex(item.Field, new BsonRegularExpression(new Regex("^" + item.Value)));
                                    }
                                }
                                break;

                            case "eq":
                                if (item.DataType == "int")
                                {
                                    if (filter == null)
                                    {
                                        filter = Builders <TEntity> .Filter.Eq(item.Field, item.intValue);
                                    }
                                    else
                                    {
                                        filter = filter & Builders <TEntity> .Filter.Eq(item.Field, item.intValue);
                                    }
                                }
                                else
                                {
                                    if (int.TryParse(item.Value, out searchval))
                                    {
                                        if (filter == null)
                                        {
                                            filter = Builders <TEntity> .Filter.Eq(item.Field, searchval);
                                        }
                                        else
                                        {
                                            filter = filter & Builders <TEntity> .Filter.Eq(item.Field, searchval);
                                        }
                                    }
                                    else
                                    {
                                        if (filter == null)
                                        {
                                            filter = Builders <TEntity> .Filter.Eq(item.Field, item.Value);
                                        }
                                        else
                                        {
                                            filter = filter & Builders <TEntity> .Filter.Eq(item.Field, item.Value);
                                        }
                                    }
                                }
                                break;

                            case "neq":
                                if (filter == null)
                                {
                                    filter = Builders <TEntity> .Filter.Ne(item.Field, item.Value);
                                }
                                else
                                {
                                    filter = filter & Builders <TEntity> .Filter.Ne(item.Field, item.Value);
                                }
                                break;

                            case "contains":
                                if (filter == null)
                                {
                                    filter = Builders <TEntity> .Filter.Regex(item.Field, "/" + item.Value + "/i");
                                }
                                else
                                {
                                    filter = (filter | Builders <TEntity> .Filter.Regex(item.Field, "/" + item.Value + "/i"));
                                }

                                int result = 0;
                                //As we are not getting DataType of field in search cases so checking that the given search value is a integer. If yes adding it as a number filter as well.
                                if ((!string.IsNullOrEmpty(item.Value)) && int.TryParse(item.Value, out result))
                                {
                                    filter = (filter | Builders <TEntity> .Filter.Eq(item.Field, result));
                                }

                                break;

                            case "endswith":
                                if (filter == null)
                                {
                                    filter = Builders <TEntity> .Filter.Regex(item.Field, new BsonRegularExpression(new Regex(item.Value + "$")));
                                }
                                else
                                {
                                    filter = filter & Builders <TEntity> .Filter.Regex(item.Field, new BsonRegularExpression(new Regex(item.Value + "$")));
                                }
                                break;

                            case "lt":
                                if (filter == null)
                                {
                                    filter = Builders <TEntity> .Filter.Lt(item.Field, item.Value);
                                }
                                else
                                {
                                    filter = filter & Builders <TEntity> .Filter.Lt(item.Field, item.Value);
                                }
                                break;

                            case "gt":
                                if (filter == null)
                                {
                                    filter = Builders <TEntity> .Filter.Gt(item.Field, item.Value);
                                }
                                else
                                {
                                    filter = filter & Builders <TEntity> .Filter.Gt(item.Field, item.Value);
                                }
                                break;

                            case "lte":
                                if (filter == null)
                                {
                                    filter = Builders <TEntity> .Filter.Lte(item.Field, item.Value);
                                }
                                else
                                {
                                    filter = filter & Builders <TEntity> .Filter.Lte(item.Field, item.Value);
                                }
                                break;

                            case "gte":
                                if (filter == null)
                                {
                                    filter = Builders <TEntity> .Filter.Gte(item.Field, item.Value);
                                }
                                else
                                {
                                    filter = filter & Builders <TEntity> .Filter.Gte(item.Field, item.Value);
                                }
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }

                //Added new sort block
                if (null != criteria.Sort)
                {
                    foreach (var sort in criteria.Sort)
                    {
                        if (!string.IsNullOrEmpty(sort.Field))
                        {
                            switch (sort.Dir)
                            {
                            case "asc":
                                if (sortDefinition == null)
                                {
                                    sortDefinition = sortDefinitionBuilder.Ascending(sort.Field);
                                }
                                else
                                {
                                    sortDefinition = sortDefinition.Ascending(sort.Field);
                                }
                                break;

                            case "desc":
                                if (sortDefinition == null)
                                {
                                    sortDefinition = sortDefinitionBuilder.Descending(sort.Field);
                                }
                                else
                                {
                                    sortDefinition = sortDefinition.Descending(sort.Field);
                                }
                                break;
                            }
                        }
                    }
                }


                FilterDefinition <TEntity> filterCriteria;
                if (filter == null)
                {
                    filterCriteria = Builders <TEntity> .Filter.Empty;
                }
                else
                {
                    filterCriteria = filter;
                }

                IFindFluent <TEntity, TEntity> cursor;

                if (sortDefinition == null)
                {
                    cursor = collection.Find(filterCriteria);
                }
                else
                {
                    cursor = collection.Find(filterCriteria).Sort(sortDefinition);
                }

                totalCount = cursor.Count();
                cursor     = cursor.Skip(pageSize * (pageNo - 1));

                if (pageSize >= 0)
                {
                    cursor = cursor.Limit(pageSize);
                }

                resultItems.AddRange(cursor.ToList());
                return(resultItems);
            }
            catch (Exception ex)
            {
                logManager.LogMessage("Message: " + ex.Message + Environment.NewLine + "StackTrace: " + ex.StackTrace + Environment.NewLine + "InnerException: " + ex.InnerException, "", "", LogLevel.Error);
                totalCount = 0;
                return(new List <TEntity>());
            }
        }