Exemplo n.º 1
0
        /// <summary>
        /// 分页查询,基础方法,参数:页面大小,页码,主键,查询字段,表达式,排序,数据总条数
        /// </summary>
        /// <param name="pageSize">页面大小</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="primaryKey">主键</param>
        /// <param name="selectFields">查询字段</param>
        /// <param name="express">表达式</param>
        /// <param name="orderBy">排序</param>
        /// <param name="recordCount">数据总条数</param>
        /// <returns></returns>
        private List <TEntity> Search(int pageSize, int pageIndex, string primaryKey, string selectFields, List <Expression> express, string orderBy, ref int recordCount)
        {
            //获取参数和条件
            CoreFrameworkEntity CoreFrameworkEntity = GetParaListAndWhere(express);

            //条件
            string where = CoreFrameworkEntity.where;
            //参数列表
            List <SqlParameter> listPara = CoreFrameworkEntity.paraList;

            selectFields = selectFields == "" ? "*" : selectFields;//查询字段

            Pager       page = new Pager();
            IDataReader sdr  = page.GetPagerInfo(tableName, primaryKey, selectFields, pageSize, pageIndex, where, orderBy, ref recordCount, listPara);

            return(DynamicBuilder <TEntity> .GetList(sdr, columnAttrList));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 不分页查询,基础方法
        /// </summary>
        /// <param name="count">显示总数,空则全部显示</param>
        /// <param name="selectFields">查询字段</param>
        /// <param name="express">表达式</param>
        /// <param name="orderBy">排序,为空则不排序</param>
        /// <returns></returns>
        private List <TEntity> Search(int?count, string selectFields, List <Expression> express, string orderBy)
        {
            //获取参数和条件
            CoreFrameworkEntity CoreFrameworkEntity = GetParaListAndWhere(express);

            //条件
            string where = CoreFrameworkEntity.where;
            //参数列表
            List <SqlParameter> listPara = CoreFrameworkEntity.paraList;

            selectFields = selectFields == "" ? "*" : selectFields;    //查询字段
            orderBy      = orderBy == "" ? "" : "order by " + orderBy; //排序

            string      topStr  = count.HasValue == false ? "" : " top " + count + " ";
            string      cmdText = "select " + topStr + selectFields + " from " + tableName + " where " + where + " " + orderBy;
            IDataReader sdr     = new SqlConvertHelper().ExecuteReader(cmdText, listPara);

            return(DynamicBuilder <TEntity> .GetList(sdr, columnAttrList));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取Lambda表达式返回的结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="func"></param>
        /// <returns></returns>
        public CoreFrameworkEntity GetLambdaEntity <T>(System.Linq.Expressions.Expression <Func <T, bool> > func)
        {
            listPara.Clear();
            CoreFrameworkEntity lambdaEntity = new CoreFrameworkEntity();

            // 二元运算符表达式
            if (func.Body is System.Linq.Expressions.BinaryExpression)
            {
                System.Linq.Expressions.BinaryExpression be = ((System.Linq.Expressions.BinaryExpression)func.Body);
                lambdaEntity.where    = BinarExpressionProvider(be.Left, be.Right, be.NodeType);
                lambdaEntity.paraList = listPara;
            }
            // 单纯的静态方法
            if (func.Body is System.Linq.Expressions.MethodCallExpression)
            {
                System.Linq.Expressions.MethodCallExpression be = ((System.Linq.Expressions.MethodCallExpression)func.Body);
                lambdaEntity.where    = ExpressionRouter(be);
                lambdaEntity.paraList = listPara;
            }
            return(lambdaEntity);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 获取表达式列表对应的参数列表和条件
        /// </summary>
        /// <param name="express"></param>
        /// <returns></returns>
        public CoreFrameworkEntity GetParaListAndWhere(List <Expression> express)
        {
            //获取实体列的特性
            List <EntityPropColumnAttributes> columnAttrList = AttributeHelper.GetEntityColumnAtrributes <TEntity>();

            #region 获取参数和条件
            //参数列表
            List <SqlParameter> listPara = new List <SqlParameter>();
            //条件
            string where = "";
            if (express != null)
            {
                if (express.Count == 0)
                {
                    where = "1=1";
                }
                int i = 0;//运行的位置,从下标0开始
                foreach (Expression exp in express)
                {
                    string fieldName = columnAttrList.Where(w => w.propName.ToLower() == exp.FieldName.ToLower()).Count() > 0
                        ? columnAttrList.Where(w => w.propName.ToLower() == exp.FieldName.ToLower()).First().fieldName : exp.FieldName;
                    //连接符号
                    if (string.IsNullOrEmpty(exp.Join))
                    {
                        //判断个数是为了防止参数名相同
                        int    fieldCount = express.Where(e => e.FieldName == exp.FieldName).Count();
                        string paraName   = fieldCount == 1 ? exp.FieldName : exp.FieldName + "_p" + i.ToStr();

                        //当两个条件没有使用连接符时,则默认用and拼接,从第一个参数后试用
                        if (i > 0)
                        {
                            //如果上一个参数不是连接符时,默认每个参数是以and的形式拼接
                            if (string.IsNullOrEmpty(express[i - 1].Join))
                            {
                                where += " and ";
                            }
                        }

                        #region 条件语句
                        switch (exp.Condition.Trim())
                        {
                        case "like":
                            listPara.Add(new SqlParameter("@" + paraName, "%" + exp.Value + "%"));
                            where += " " + fieldName + " like @" + paraName;
                            break;

                        case "in":
                            where += " " + fieldName + " in (" + exp.Value + ")";
                            break;

                        default:
                            listPara.Add(new SqlParameter("@" + paraName, exp.Value));
                            where += " " + fieldName + " " + exp.Condition + " @" + paraName;
                            break;
                        }
                        #endregion
                    }
                    else
                    {
                        where += " " + exp.Join + " ";
                    }
                    i++;
                }
            }
            else
            {
                where = "1=1";
            }
            #endregion

            CoreFrameworkEntity entity = new CoreFrameworkEntity();
            entity.paraList = listPara;
            entity.where    = where;
            return(entity);
        }