示例#1
0
        /// <summary>
        /// 根据传入的条件对表查询并返回对象集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public virtual List <T> LoadByCondition <T>(KeyValuePair <string, object>[] conditions, Expression <Func <T, object> > orderBy = null, string orderType = "asc") where T : ModelBase
        {
            List <T>      result = new List <T>();
            Type          t      = typeof(T);
            List <string> orders = new List <string>();

            if (orderBy != null)  //有传排序属性就按排序属性排序
            {
                orders.Add(ExpressionHandle.DealGetPropertyNameExpression <T>(orderBy));
            }
            else    //否则按主键排序
            {
                var props = t.GetProperties();
                foreach (var p in props)
                {
                    if (p.IsDefined(typeof(MyPrimaryKeyAttribute)))
                    {
                        orders.Add(p.Name);
                    }
                }
            }

            // 获取作为条件的属性的属性名
            string[] propNms = GetKeysArray(conditions);

            string[] order = orders.ToArray();
            string   sql   = stringBuilder.SelectByCondition(GetTableName(t), propNms, order, orderType);

            OpenConnection();
            var table = helper.DoSelect(sql, conditions);

            initObjectList <T>(result, table);
            helper.ShutDown();
            return(result);
        }
示例#2
0
        /// <summary>
        /// 根据C#表达式构造SQL语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="express"></param>
        /// <returns></returns>
        public virtual KeyValuePair <string, object>[] SelectMany <T>(out string sql, Expression <Func <T, bool> > express, string TableName)
        {
            var    ex       = express.Body;
            string sqlBase  = SelectMany_Base(TableName);
            string whereStr = "";

            var ret = ExpressionHandle.DealExpression(out whereStr, ex);

            sql = sqlBase + whereStr;
            return(ret);
        }
示例#3
0
        /// <summary>
        /// 对表进行分页查询并返回对象集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="nowPage">目前在第几页</param>
        /// <param name="infoCount">记录总数</param>
        /// <param name="condition">查询条件</param>
        /// <param name="orderBy">排序</param>
        /// <param name="orderType">排序类型:asc,desc</param>
        /// <returns></returns>
        public virtual List <T> LoadPageList <T>(int pageSize, int nowPage, ref int infoCount, KeyValuePair <string, object>[] conditions = null, Expression <Func <T, object> > orderBy = null, string orderType = "asc") where T : ModelBase
        {
            var result = new List <T>();

            OpenConnection();
            var           tragetType = typeof(T);
            List <string> orders     = new List <string>();

            if (orderBy != null)  //有传排序属性就按排序属性排序
            {
                orders.Add(ExpressionHandle.DealGetPropertyNameExpression <T>(orderBy));
            }
            else    //否则按主键排序
            {
                Type t     = typeof(T);
                var  props = t.GetProperties();
                foreach (var p in props)
                {
                    if (p.IsDefined(typeof(MyPrimaryKeyAttribute)))
                    {
                        orders.Add(p.Name);
                    }
                }
            }
            string[] order = orders.ToArray();
            string   sql   = "";// BuildPageSelectSql();

            string[] propNms = GetKeysArray(conditions);
            if (conditions == null)
            {
                sql = stringBuilder.SelectPageList(tragetType.Name, pageSize, nowPage, order, orderType);
            }
            else
            {
                sql = stringBuilder.SelectPageListWithCondition(tragetType.Name, pageSize, nowPage, propNms, order, orderType);
            }
            var table = helper.DoSelect(sql, conditions);

            initObjectList <T>(result, table);
            if (table.Rows.Count > 0)
            {
                infoCount = Convert.ToInt32(table.Rows[0]["cnt"]);
            }
            helper.ShutDown();
            return(result);
        }
示例#4
0
        public virtual KeyValuePair <string, object>[] SelectPageListWithCondition <T>(out string sql, string Table, int pageSize, int nowPage, Expression <Func <T, bool> > condition, string[] orderBy = null, string orderType = "asc")
        {
            StringBuilder sb       = new StringBuilder();
            string        whereStr = "";

            KeyValuePair <string, object>[] conditions = null;
            sb.Append("select * from " + Table + ",(select count(*) as cnt from " + Table + ") as T" + " where ");
            if (condition != null)
            {
                conditions = ExpressionHandle.DealExpression(out whereStr, condition.Body);
                sb.Append(whereStr);
                sb.Append(" and ");
            }
            sb.Append("1=1 ");
            sql = sb.ToString() + OrderByString(orderType, orderBy) + LimitString(pageSize, nowPage);
            return(conditions);
        }
示例#5
0
        private void btn_Count_Click(object sender, RoutedEventArgs e)
        {
            txt_AnswerTextBlock.Text = "";
            ExpressionHandle hl      = new ExpressionHandle();
            List <RealNum>   ListExp = new List <RealNum>();
            BaseEquation     eqtools = new BaseEquation();

            if (!eqtools.BalanceChecker(inputBox.Text))
            {
                showInformation("ERROR", "表达式括号不平衡");
                return;
            }
            ListExp = hl.MakeEasy(inputBox.Text);
            string result = GetResult(ListExp);

            txt_AnswerTextBlock.Text = result;
        }
示例#6
0
        public override KeyValuePair <string, object>[] SelectPageListWithCondition <T>(out string sql, string Table, int pageSize, int nowPage, Expression <Func <T, bool> > condition, string[] orderBy = null, string orderType = "asc")
        {
            KeyValuePair <string, object>[] ret = null;
            string        whereStr = "";
            StringBuilder sb       = new StringBuilder();

            sb.Append(" where ");
            if (condition != null)
            {
                ret = ExpressionHandle.DealExpression(out whereStr, condition.Body);
                sb.Append(whereStr);
                sb.Append(" and ");
            }
            sb.Append("1=1 ");
            whereStr = sb.ToString();
            int star = (nowPage - 1) * pageSize + 1;
            int end  = star + pageSize - 1;

            sql = "select * from(select ROW_NUMBER()over(" + OrderByString(orderType, orderBy) + ") rowID,* from " + Table + " " + whereStr + ")  as tbq,(select count(*) cnt from " + Table + ") as tb where tbq.rowID between " + star + " and " + end + ";";
            return(ret);
        }
		public WhereClause(ExpressionHandle<bool> predicate) {
			this.predicate = predicate;
		}