我有一個問題,很遺憾找不到解決方案!這是我的資料庫:
在此處輸入影像描述
我想將所有 PaymentSum(double) [PaymentInformation] 存盤在 Total (double) [Document] 中。
這是 SQL 陳述句:
Select SUM(PaymentSum)
from PaymentInformations
where DocumentId = 1;
我試過這個,但沒有成功:
// POST: api/PaymentInformations
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost("new/{eId}")]
public async Task<ActionResult<PaymentInformation>> PostpaymentInformations(long eId, PaymentInformation paymentInformation)
{
Document document = await _context.Documents.FindAsync(eId);
document.Total = _context.PaymentInformations
.Where(b => b.Document.Id == eId)
.Sum(a => a.PaymentSum);
//document.Total = _context.PaymentInformations.FromSqlRaw("Select SUM(PaymentSum) from PaymentInformations where DocumentId = {0}",eId).FirstOrDefault();
foreach (var item in _context.PaymentInformations)
{
System.Console.WriteLine(item.PaymentSum);
System.Console.WriteLine(item.DocumentId);
}
_context.PaymentInformations.Add(paymentInformation);
document.addPaymentInformation(paymentInformation);
await _context.SaveChangesAsync();
return CreatedAtAction("GetpaymentInformations", new { id = paymentInformation.Id }, paymentInformation);
}
我希望有一個人可以幫助我。
謝謝!!
uj5u.com熱心網友回復:
看起來您正在正確設定 document.Total,當您進一步呼叫 document.addPaymentInformation(paymentInformation) 時,您是否可能覆寫了 Total 值?
uj5u.com熱心網友回復:
我設定了一個斷點,它表明 id 匹配
在此處輸入影像描述
轉機不成功
在此處輸入影像描述
uj5u.com熱心網友回復:
public void addPaymentInformation(PaymentInformation paymentInformation)
{
if (PaymentInformations == null)
{
PaymentInformations = new List<PaymentInformation>();
}
paymentInformation.Document = this;
PaymentInformations.Add(paymentInformation);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/463236.html
上一篇:使用LINQ創建特定資料集
