我需要将listview中多列的值存储到我的数据库的单个条目中。 示例
ID | Description | Price
1 | Big | 20
2 | Large | 40
3 | Small | 60
我想将值(大,大,小)存储到单独的表
ID | Description | Price
1 | Big, Large, Small | 120
ive tried using
foreach (ListView s in ????? )
{
}
请帮帮我...... :)
答案 0 :(得分:0)
您可以使用以下代码代替foreach。
class Product
{
public int ID { get; set; }
public string Description { get; set; }
public int Price { get; set; }
}
将测试数据分配到列表
List<Product> list = new List<Product>() {
new Product() { ID = 1, Description = "BBB" , Price = 10},
new Product() { ID = 2, Description = "CCC" , Price = 40},
new Product() { ID = 3, Description = "DDD" , Price = 60}};
List<Product> newList = new List<Product>();
newList.Add(new Product() { ID = list[0].ID , Description = string.Join(", ", list.Select(a => a.Description).ToArray()),
Price = list.Select(a => a.Price).Sum()});
此外,如果您想从ListView获取值,您可以使用以下代码。
var p = new Product()
{
ID = Convert.ToInt32((ListView1.Items[0].FindControl("lblID") as Label).Text),
Description = string.Join(", ", (ListView1.Items.Select(a => (a.FindControl("lblDescription") as Label).Text)).ToArray()),
Price = ListView1.Items.Select(a => Convert.ToInt32((a.FindControl("lblPrice") as Label).Text)).Sum()
};
此外,您还必须将“lblID”替换为您的标签。