我想知道你是否帮助我?
我有一个由物品,价格和数量组成的数组。 如果该项目存在于数组中,则必须更新价格和数量,如果它没有退出,则必须添加
这是我尝试过的代码:
if (line.Contains(ItemCode))
{
string[] details = line.Split(new string[] { "|" }, StringSplitOptions.None);
{
for (int i = 0; i < details.Length; i++)
{
if (details[i].Contains(ItemCode))
{
string[] line_details = details[i].Split(',');
string replace = line_details[2].Trim() + "," + line_details[3].Trim();
double NewQty = double.Parse(Qty) + double.Parse(line_details[2]);
double NewPrice = (double.Parse(UnitPrice) * double.Parse(Qty));
double NewUnitPrice = NewPrice + double.Parse(line_details[3]);
string new_replace = NewQty + "," + NewUnitPrice;
line = line.Replace(replace, new_replace);
}
}
}
}
else
{
line = line + "\"Detail\",0," + Qty + "," + (double.Parse(UnitPrice) * double.Parse(Qty)) + "," + InclusivePrice + ",\"" + UnitUsed + "\"," + TaxType + "," + DiscountType + "," + DiscountPercentage + ",\"" + ItemCode + "\",\"" + Description + "\"," + SearchType + "," + "\"\"" + ",\"" + MultiStore + "\"|" + Environment.NewLine;
}
它不起作用你能不能帮助我吗?
答案 0 :(得分:2)
初始化后,C#中的数组不能添加任何条目。您最好使用List<String>
,您可以在其中添加和删除列表中的条目。或者考虑使用Dictionary<Int32, String>
,这样可以使用ItemCode
作为标识符,以便更轻松地查找给定条目。
作为一个重点,不是将所有商品数据存储在分隔的字符串中,而是为它们创建一个新类,并将各种细节作为属性,然后您可以使用理论Dictionary<Int32, ItemObject>
来更清晰。