首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Hashtable循环遍历有关问题

2012-09-19 
Hashtable循环遍历问题Hashtable ht (Hashtable)Session[UserList]我想循环遍历我的hashtabale中的ke

Hashtable循环遍历问题
Hashtable ht = (Hashtable)Session["UserList"];
我想循环遍历我的hashtabale中的key值,然后循环遍历出来,用string接收,出现这种效果,如果为1个,则只是1,如果多个则用,隔开,形成这样的效果1,2,3

[解决办法]
string result = "";
if (ht.Count == 1)
result = ht[0].ToString();
else
{
for (int i = 0; i < ht.Count; i++)
{
if (i == ht.Count - 1)
{
result += ht[i].ToString()
}
else
{
result += ht[i].ToString() + ",";
}
}
}
[解决办法]

C# code
 Hashtable ht = new Hashtable();                ht.Add("1t", "1");                ht.Add("2t", "1");                StringBuilder sb = new StringBuilder();                foreach (var item in ht.Keys)                    sb.Append(item.ToString()+",");                string result = sb.ToString().TrimEnd(',');//1t,2t
[解决办法]
string str = string.Join(",", ht.Cast<DictionaryEntry>().Select((key, value) => value.ToString()).ToArray());

热点排行