コード例 #1
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());
        }