简体   繁体   中英

Entity Framework getting a running total from columns

This has probably been asked before, but I am struggling to find an answer. I have the below code, trying to get a running total.

This is not an error that I have seen before and stumped as to what it actually means, or how to fix it, and trying to keep this in one query if possible. However, will be open to better ways to do this.

I have added a comment where the error is occurring and what the error is inside Visual Studio.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

    class VMAccountWindow : BaseViewModel
    {

        public VMAccount()
        {
            using (DBContext db = new DBContext())
            {
                decimal currentTotal = 0;

                var tl = db.Transaction
                    .OrderByDescending(
                        p => p.TransactionDate
                    )
                    .Select(p => new TransactionList
                    {
                        AccountId = p.AccountId,
                        TransactionDate = p.TransactionDate,
                        Deposit = p.TransCode == "Deposit" ? p.TransactionAmount.ToString() : "",
                        Withdrawal = p.TransCode == "Deposit" ? "" : p.TransactionAmount.ToString(),
                        Notes = p.Notes,
                        // Following gives an error about Expression tree may not contain an assignment operator
                        AccountBalance = currentTotal += p.TransCode == "Deposit" ? p.TransactionAmount : -p.TransactionAmount
                    })
                .Where(o => o.AccountId == 1)
                .ToList();
            }
        }
    }

    public class TransactionList
    {
        public int AccountId { get; set; }
        public string TransactionDate { get; set; }
        public string Deposit { get; set; }
        public string Withdrawal { get; set; }
        public string Notes { get; set; }
        public decimal AccountBalance { get; set; }
    }

Any help would be appreciated.

The solution was to turn the results into a list first, then do the second select, and I had not seen this as the searches I had done only shown get data. Without showing how.

As this may trip other new developers up, I have pasted the working example below, this is a result of all the suggestions from those who have tried to help.

var tl = db.Transaction
    .Where(o => o.AccountId == 1)
    .OrderBy(p => p.TransactionDate)
    .ToList()
    .Select(i =>
            {
                currentTotal += p.TransCode == "Deposit" ? p.TransactionAmount : 0;
                return new TransactionList
                {
                    ToAccountId = i.ToAccountId,
                    TransactionDate = i.TransactionDate,
                    AccountBalance = currentTotal
                };
            }
    )
    .ToList();

Unfortunately, I do not know the reason why this has to be turned to a list first, then do the select, so if anyone can chime in as to the reason, it would be appreciated if someone who knows could explain that part.

You are using += after initially assigning currentTotal

Change it to + like below:

AccountBalance = currentTotal + p.TransCode == "Deposit" ? p.TransactionAmount : -p.TransactionAmount                    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM