【发布时间】:2020-09-13 04:26:20
【问题描述】:
这可能以前被问过,但我正在努力寻找答案。我有下面的代码,试图得到一个运行总数。
这不是我以前见过的错误,并且对于它的实际含义或如何修复它感到困惑,并尽可能将其保留在一个查询中。但是,我们愿意接受更好的方法来做到这一点。
我已在发生错误的位置以及 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; }
}
任何帮助将不胜感激。
【问题讨论】:
-
如果您尝试选择匿名类型并仅选择 currenttotal。还是错误?
-
是的,就像链接中的here 一样,无论如何,我在使用 EF6 时总是会遇到同样的错误。
标签: c# wpf entity-framework-6