コード例 #1
0
ファイル: SortInfoExtension.cs プロジェクト: manyar98/Zhivar
        public static IOrderedQueryable <T> CreateOrderedQuery <T>(
            this List <SortInfo> sortInfoList,
            IQueryable <T> query)
            where T : class
        {
            bool flag = true;
            IOrderedQueryable <T> source = (IOrderedQueryable <T>)null;

            foreach (SortInfo sortInfo in sortInfoList)
            {
                string mapPropertyName = PropertyMapCollection.GetMapPropertyName <T>(sortInfo.Field);
                try
                {
                    if (flag)
                    {
                        source = !(sortInfo.Dir.ToLower() == "asc") ? query.OrderByDescending <T>(mapPropertyName) : query.OrderBy <T>(mapPropertyName);
                        flag   = false;
                    }
                    else
                    {
                        source = !(sortInfo.Dir.ToLower() == "asc") ? source.ThenByDescending <T>(mapPropertyName) : source.ThenBy <T>(mapPropertyName);
                    }
                }
                catch (ArgumentException ex)
                {
                }
            }
            return(source);
        }
コード例 #2
0
ファイル: FilterInfo.cs プロジェクト: manyar98/Zhivar
        public Expression <Func <TEntity, bool> > TranslateFilter <TEntity>(
            bool processPersianChars = true)
            where TEntity : class
        {
            QueryObject <TEntity> queryObject         = new QueryObject <TEntity>();
            ParameterExpression   parameterExpression = Expression.Parameter(typeof(TEntity), "entity");

            foreach (FilterData filter in this.Filters)
            {
                if (filter.Filters != null && filter.Filters.Any <FilterData>())
                {
                    Expression <Func <TEntity, bool> > query = new FilterInfo()
                    {
                        Logic   = filter.Logic,
                        Filters = filter.Filters
                    }.TranslateFilter <TEntity>(true);
                    if (string.IsNullOrWhiteSpace(this.Logic))
                    {
                        queryObject.And(query);
                    }
                    else if (this.Logic.ToLower() == "and")
                    {
                        queryObject.And(query);
                    }
                    else
                    {
                        queryObject.Or(query);
                    }
                }
                else if (processPersianChars && filter.Value != null && (filter.Value.Contains("ي") || filter.Value.Contains("ك") || filter.Value.Contains("ی") || filter.Value.Contains("ک")))
                {
                    FilterInfo filterInfo = new FilterInfo()
                    {
                        Logic = "or"
                    };
                    filterInfo.Filters.Add(filter);
                    string str = "";
                    if (filter.Value.Contains("ي") || filter.Value.Contains("ك"))
                    {
                        str = filter.Value.Replace("ي", "ی").Replace("ك", "ک");
                    }
                    else if (filter.Value.Contains("ی") || filter.Value.Contains("ک"))
                    {
                        str = filter.Value.Replace("ی", "ي").Replace("ک", "ك");
                    }
                    filterInfo.Filters.Add(new FilterData()
                    {
                        Field    = filter.Field,
                        Logic    = filter.Logic,
                        Operator = filter.Operator,
                        Value    = str
                    });
                    Expression <Func <TEntity, bool> > query = filterInfo.TranslateFilter <TEntity>(false);
                    if (string.IsNullOrWhiteSpace(this.Logic))
                    {
                        queryObject.And(query);
                    }
                    else if (this.Logic.ToLower() == "and")
                    {
                        queryObject.And(query);
                    }
                    else
                    {
                        queryObject.Or(query);
                    }
                }
                else
                {
                    string     mapPropertyName = PropertyMapCollection.GetMapPropertyName <TEntity>(filter.Field);
                    Expression bodyExpression  = FilterInfo.CreateBodyExpression(parameterExpression, mapPropertyName, filter.Operator, filter.Value);
                    if (bodyExpression != null)
                    {
                        Expression <Func <TEntity, bool> > query = Expression.Lambda <Func <TEntity, bool> >(bodyExpression, parameterExpression);
                        if (string.IsNullOrWhiteSpace(this.Logic))
                        {
                            queryObject.And(query);
                        }
                        else if (this.Logic.ToLower() == "and")
                        {
                            queryObject.And(query);
                        }
                        else
                        {
                            queryObject.Or(query);
                        }
                    }
                }
            }
            return(queryObject.Query());
        }