2015-09-19 50 views
1

的消息,我得到:格式复制消息

清单包含LCD加油机不足物品,库存 包含足够的物品塔饮水机

的消息我会想到:

库存包含不足的项目用于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)); 
    } 
} 

回答

1

在其中遇到错误每次迭代中,仅添加productnameerrors阵列,而不是整个错误消息。

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第一个参数是所述分离器,在这种情况下','。第二个参数是要连接的值的数组(由指定的分隔符分隔)。