【问题标题】:Tracking Account balance and information跟踪账户余额和信息
【发布时间】:2011-10-03 18:17:49
【问题描述】:

我正在使用 MVVM 和 LinqToSql 开发一个简单的金融应用程序。我正在尝试找出构建数据的最佳方式。这是我所拥有的(简化的):

帐户

  • 帐户 ID (PK)
  • 帐户名
  • AccountBalance(返回交易列的总和)
  • 事务(实体集)

我正在显示给定帐户的交易列表,但遇到了一些麻烦。我想做的是显示按日期分组的交易。为此,我使用了允许分组的 LongListSelector 控件。控件的数据源如下:

var transByDate = from trans in App.ViewModel.AllTransactions
                  trans by trans.TransDate.ToString("d") into t
                  t.Key ascending
                  new Transaction<Transaction>(t.Key, t);

this.lls_transByDate.ItemsSource = transByDate;

这行得通,我看到我的组标题和日期,下面有当天的交易数据。

我遇到的问题是在每个日期的标题中显示每日余额。如何构建我的数据,以便按日期轻松访问帐户余额,但可以更新(如果用户返回 2 周并更改现有交易)。

编辑我想看到的:

[10/2/2011 -------------- $753.23]

交易 1 - 杂货店 - 30.00 美元

[10/1/2011 -------------- $723.23]

银行 - 车辆 - $400.00

商店 - 杂货店 - 35.00 美元

[09/31/2011 -------------- $288.23]

【问题讨论】:

    标签: c# silverlight windows-phone-7 mvvm


    【解决方案1】:

    在我的Transaction&lt;T&gt; 类中,我添加了另一个名为TransAmount 的属性。因此,当从上面的代码块调用构造函数时,这就是它的作用。

    public Transaction(string transDate, IEnumerable<T> items)
    {
        DateTime tmpDate = DateTime.Parse(transDate);
    
        var deposits = from t in App.ViewModel.AllTransactions
                            where t.TransDate <= new DateTime(tmpDate.Year, tmpDate.Month, tmpDate.Day, 23, 59, 59, 999)
                            where t.TransType == (int)TransType.Deposit
                            select t.TransAmount;
    
        var withdrawals = from t in App.ViewModel.AllTransactions
                            where t.TransDate <= new DateTime(tmpDate.Year, tmpDate.Month, tmpDate.Day, 23, 59, 59, 999)
                            where t.TransType == (int)TransType.Withdrawal
                            select t.TransAmount;
    
        decimal total = (deposits.Sum() - withdrawals.Sum());
    
        this.TransAmount = total;
        this.TransDate = transDate;
        this.Items = new List<T>(items);
    }
    

    然后我只需将我的 XAML 绑定到该属性。也许有一种更简洁的方法,但我认为重新计算余额比将余额存储在数据库中更干净。

    【讨论】:

      猜你喜欢
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 2013-09-23
      • 2011-12-09
      • 2016-10-11
      相关资源
      最近更新 更多