我收到的消息:
库存包含LCD Dispenser,Inventory的物品不足 包含塔式分配器的物品不足
我期待的信息:
库存包含LCD分配器塔的物品不足 分配器
List<string> errors = new List<string>();
for (int index = 0; index < this.gridagree.Rows.Count; index++)
{
int productId = Convert.ToInt32(gridagree.Rows[index].Cells[3].Text);
string productname = gridagree.Rows[index].Cells[4].Text;
int quantityRequested = Convert.ToInt32(gridagree.Rows[index].Cells[5].Text);
int availableQuantity = Convert.ToInt32(s.getprodqun(productId));
if (quantityRequested > availableQuantity)
{
errors.Add(string.Format("Inventory contains insufficient items for {0} ", productname));
}
}
答案 0 :(得分:1)
在遇到错误的每次迭代中,只将productname
添加到errors
数组,而不是整个错误消息。
List<string> errors = new List<string>();
for (int index = 0; index < this.gridagree.Rows.Count; index++)
{
int productId = Convert.ToInt32(gridagree.Rows[index].Cells[3].Text);
string productname = gridagree.Rows[index].Cells[4].Text;
int quantityRequested = Convert.ToInt32(gridagree.Rows[index].Cells[5].Text);
int availableQuantity = Convert.ToInt32(s.getprodqun(productId));
if (quantityRequested > availableQuantity)
{
errors.Add(productname);
}
}
然后,使用string.Join
将它们连接成一条错误消息。
var errorMessage = string.Format("Inventory contains insufficient items for {0} ",
string.Join(',', errors));
string.Join
的第一个参数是分隔符,在本例中为','
。第二个参数是要连接的值数组(由指定的分隔符分隔)。