0
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();

StockListこのコードは、n行ある場合、すべてのn回の最初の行のみを追加します。

コードの何が問題になっていますか?

4

1 に答える 1

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();
}
于 2012-05-09T16:28:53.590 に答える