Home > Software design >  ASP.NET Core Linq Compare Id and Sum value
ASP.NET Core Linq Compare Id and Sum value

Time:04-08

I have a problem and unfortunately can not find the solution! This is my database:

enter image description here

and I would like to store all PaymentSum(double) [PaymentInformation] in the Total (double) [Document].

This is the SQL statement:

Select SUM(PaymentSum)
from PaymentInformations
where DocumentId = 1;

I have tried this, but without success:

// 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);
       
    }

I hope someone can help me.

Thank you!!

CodePudding user response:

It looks like you are setting document.Total correctly, are you perhaps overriding the Total value when you call document.addPaymentInformation(paymentInformation) further down?

CodePudding user response:

I set an breakpoint and it has shown that the id matches

enter image description here

transfer without success

enter image description here

  • Related