我有一个列表,它有字典。现在我想回顾字典的所有价值如何? 每当我尝试运行它不显示在item.like如何从字典类型列表中检索数据?
foreach(Dictionary dict in products)
类型的词典我用来做它在Java列表中foreach
,但我不能这样做在C#为什么呢?那么解决方案是什么?
代码波纹管
List<Dictionary<String,String>> products=new List<Dictionary<String,String>>();
products=loader.filterproductsbysubcat(subcat);
and here is the generated dictionary list
List<Dictionary<String, String>> productlist = new List<Dictionary<string, string>>();
String qry = "select product_tbl.Id,product_tbl.brandid,product_tbl.gender,product_tbl.info,product_tbl.name,product_tbl.price,product_tbl.productcode,product_tbl.status,image_tbl.imagename,image_tbl.imagepath from product_tbl left join image_tbl on product_tbl.Id=image_tbl.productid where product_tbl.subcat='"+subcategoryid+"'";
dt = db.ConnectDataBaseReturnDT(qry);
foreach (DataRow row in dt.Rows)
{
Dictionary<String, String> mydictionary = new Dictionary<string, string>();
mydictionary.Add("id",row["Id"].ToString());
mydictionary.Add("brandid", row["brandid"].ToString());
mydictionary.Add("gender", row["gender"].ToString());
mydictionary.Add("info", row["info"].ToString());
mydictionary.Add("name", row["name"].ToString());
mydictionary.Add("price", row["price"].ToString());
mydictionary.Add("productcode", row["productcode"].ToString());
mydictionary.Add("status", row["status"].ToString());
mydictionary.Add("imagename", row["imagename"].ToString());
mydictionary.Add("imagepath", row["imagepath"].ToString());
productlist.Add(mydictionary);
}
return productlist;
你为什么不使用'DataTable'?每个DataRow使用一个字典的奇怪方法。即使它是一种可能导致讨厌问题的不同类型,也会将所有内容都转换为字符串。 –