コード例 #1
0
        public async Task <IEnumerable <NameItem> > GetNameItemsAsync(string keyField, string nameField, string tableName, object whereParam = null, string orderBy = null, int count = 0)
        {
            if (!string.IsNullOrEmpty(orderBy))
            {
                orderBy = "ORDER BY " + orderBy;
            }
            string top = string.Empty;

            if (count > 0)
            {
                top = string.Format("top ({0})", count);
            }

            var whereAndParam = DapperExtension.GetWhereSqlAndParameters(whereParam);

            var sql = string.Format("select {0} [{1}] as Id ,[{2}] as Name from {3} where {4} {5}", top, keyField, nameField, tableName,
                                    whereAndParam.Item1, orderBy);

            using (var con = this.ConnectionFactory.OpenConnection())
            {
                var list = await con.QueryAsync <NameItem>(sql, whereAndParam.Item2);

                return(list.Distinct());
            }
        }
コード例 #2
0
        public async Task <IEnumerable <SelectListItem> > GetSelectListAsync(string valueField, string textField, string tableName, object whereParam = null, string orderBy = null)
        {
            if (!string.IsNullOrEmpty(orderBy))
            {
                orderBy = "ORDER BY " + orderBy;
            }

            var whereAndParam = DapperExtension.GetWhereSqlAndParameters(whereParam);

            var sql = string.Format("select [{0}] as Value ,[{1}] as Text from {2} where {3} {4}", valueField, textField, tableName,
                                    whereAndParam.Item1, orderBy);

            IEnumerable <SelectListItem> list = null;

            using (var con = this.ConnectionFactory.OpenConnection())
            {
                list = await con.QueryAsync <SelectListItem>(sql, whereAndParam.Item2);
            }
            return(list);
        }