Exemple #1
0
        public static bool ToWhereClause(FilterSql context, List <FilterCriteria> findIn, Predicate <FilterCriteria> match)
        {
            if (context.Criteria != null)
            {
                context.Criteria.Clear();   // !
            }
            context.Criteria = findIn.FindAll(match);

            return(ToWhereClause(context));
        }
Exemple #2
0
        public static IQueryable ToFilter <TEntity>(this IQueryable query, List <FilterCriteria> criteria, string entityName)
        {
            var sql = new FilterSql();

            if (ToWhereClause(sql, criteria, c => !c.IsInactive && c.Entity.IsCaseInsensitiveEqual(entityName)))
            {
                //query 保证存在 entity 实体关联,否则 entity.name 不成立
                //WhereClause= " or entity.name =@{0}" EF 生成的SQL语句格式,非多表查询,可省略entity
                //Values="123"
                query = query.Where(sql.WhereClause.ToString(), sql.Values.ToArray());
            }

            return(query);
        }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static bool ToWhereClause(FilterSql context)
        {
            if (context.Values == null)
            {
                context.Values = new List <object>();
            }
            else
            {
                context.Values.Clear();
            }

            if (context.WhereClause == null)
            {
                context.WhereClause = new StringBuilder();
            }
            else
            {
                context.WhereClause.Clear();
            }

            int index = 0;

            FilterParentheses(context.Criteria);

            foreach (var itm in context.Criteria)
            {
                if (context.WhereClause.Length > 0)
                {
                    context.WhereClause.AppendFormat(" {0} ", itm.Or ? "Or" : "And");
                }

                if (itm.Open.HasValue && itm.Open.Value)
                {
                    context.WhereClause.Append("(");
                }


                if (!itm.Value.HasValue())
                {
                    context.WhereClause.AppendFormat("ASCII({0}) Is Null", itm.SqlName);        // true if null or empty (string)
                }
                else
                {
                    context.WhereClause.Append(itm.SqlName);

                    if (itm.Operator == FilterOperator.Contains)
                    {
                        context.WhereClause.Append(".Contains(");
                    }
                    else if (itm.Operator == FilterOperator.StartsWith)
                    {
                        context.WhereClause.Append(".StartsWith(");
                    }
                    else if (itm.Operator == FilterOperator.EndsWith)
                    {
                        context.WhereClause.Append(".EndsWith(");
                    }
                    else
                    {
                        context.WhereClause.AppendFormat(" {0} ", itm.Operator == null ? "=" : itm.Operator.ToString());
                    }

                    context.WhereClause.Append(FormatParameterIndex(ref index));

                    if (itm.Operator == FilterOperator.Contains || itm.Operator == FilterOperator.StartsWith || itm.Operator == FilterOperator.EndsWith)
                    {
                        context.WhereClause.Append(")");
                    }

                    context.Values.Add(FilterValueToObject(itm.Value, itm.Type));
                }

                if (itm.Open.HasValue && !itm.Open.Value)
                {
                    context.WhereClause.Append(")");
                }
            }
            return(context.WhereClause.Length > 0);
        }