Dapper查询返回数组

 

        /// <summary>
        /// 根据用户昵称查询OpenID,模糊查询可能查出多个OpenID
        /// </summary>
        /// <param name="nickName"></param>
        /// <returns></returns>
        public string[] GetOpenIDFromWxUsersByNickName(string nickName)
        {
            //var sql = "select OpenID from wxUsers where NickName = '@NickName'";

            //测试
            var sql = " select UserID,OpenID,NickName from wxUsers where NickName like '%jank%'";
            var openIdArray = _dbServices.Db.Query<string>(sql, new { NickName = nickName }).ToArray();
            return openIdArray;
        }

 

像这样的就可以成功,如果将Query<string>改为Query<string[]>就会返回string[][]

返回string[][]虽然编译通过,但是运行不成功。

Query<string>只返回第一个字段的数组,也就是只返回UserID的数组。

 

查询出字段数组可以用来遍历

            //微信昵称
            string NickName = context.Request["NickName"];
            if (!RegExp.IsNull(context.Request["NickName"]))
            {
                string[] openIdArray = _otherServices.GetOpenIDFromWxUsersByNickName(NickName);
                for (int i = 0; i < openIdArray.Length; i++)
                {
                    OpenIdSb.Append("'"+openIdArray[i] + "',");
                }
                OpenIdWhereIn = OpenIdSb.ToString().Substring(0, OpenIdSb.Length - 1);
            }

 

posted on 2022-09-20 16:10  Jankie1122  阅读(46)  评论(0编辑  收藏  举报