2015-09-19 54 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第一個參數是所述分離器,在這種情況下','。第二個參數是要連接的值的數組(由指定的分隔符分隔)。