コード例 #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>
        /// 对表进行分页查询并返回对象集合
        /// </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);
        }