ASP.NET Core Linq比较Id和Sum值



我遇到了一个问题,很遗憾找不到解决方案!这是我的数据库:

在此处输入图像描述

并且我想将所有PaymentSum(double([PaymentInformation]存储在Total(double[文档]中。

这是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);

}

我希望有人能帮助我。

谢谢!!

看起来您正在设置文档。Total是正确的,当您进一步调用document.addPaymentInformation(paymentInformation(时,您可能会覆盖Total值吗?

我设置了一个断点,它显示id与匹配

在此处输入图像描述

无成功转移

在此处输入图像描述

public void addPaymentInformation(PaymentInformation paymentInformation)
{
if (PaymentInformations == null)
{
PaymentInformations = new List<PaymentInformation>();
}
paymentInformation.Document = this;
PaymentInformations.Add(paymentInformation);
}

相关内容

  • 没有找到相关文章

最新更新