ConcurrentDictionary<string, ConcurrentQueue<string>> 1个key 对应多个value

如上图,需要将 groups的值作为key,将id 作为value 进行存放,生成一个键值对。

string sqlConn = "Data Source=127.0.0.1;Initial Catalog=数据库ming;User ID=用户名;Password=密码";
            using (SqlConnection conn = new SqlConnection(sqlConn))
            {
                conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句
                SqlCommand cmd = conn.CreateCommand();
                string getIds = @"select id,groups from XXX";
                cmd.CommandText = getIds;
                SqlDataReader read = cmd.ExecuteReader();

                List<string> ids = new List<string>();
                while (read.Read())
                {
                    ConcurrentQueue<string> co = IdsList.GetOrAdd(read["groups"].ToString(), new ConcurrentQueue<string>());
                    co.Enqueue(read["id"].ToString());
                }
                read.Close();               
            }

这样就可以得到结果。

猜你喜欢

转载自www.cnblogs.com/hllxy/p/12171648.html