foreach (StockItem item in StockList)
{
Master master = new Master();
master.VoucherNo = BillNo;
master.Voucher = "Sales";
master.StockName = StockList[0].StockName;
master.Quantity = StockList[0].Quantity;
master.Unit = StockList[0].Unit;
master.Price = StockList[0].UnitPrice;
master.Amount = StockList[0].Amount;
dbContext.AddToMasters(master);
dbContext.SaveChanges();
}
Sale sale = new Sale();
sale.InvoiceNo = BillNo;
sale.Date = BillDate;
sale.Party = Customer;
sale.Amount = (decimal)TotalAmount;
dbContext.AddToSales(sale);
dbContext.SaveChanges();
如果有n行,此代码只会添加StockList
的第一行n次。
代码有什么错误?
答案 0 :(得分:2)
您正在迭代StockList,但实际上并没有使用迭代变量。
你使用StockList [0]的任何地方都应该使用item。
编辑:这是你的循环应该是什么样的:
foreach (StockItem item in StockList)
{
Master master = new Master();
master.VoucherNo = BillNo;
master.Voucher = "Sales";
master.StockName = item.StockName;
master.Quantity = item.Quantity;
master.Unit = item.Unit;
master.Price = item.UnitPrice;
master.Amount = item.Amount;
dbContext.AddToMasters(master);
dbContext.SaveChanges();
}