2011-10-03 95 views
2

我正在使用MVVM & LinqToSql进行简单的财务应用程序。我试图找出构建数据的最佳方法。下面是我所(简化):跟踪帐户余额和信息

帐户

  • ACCOUNTID(PK)
  • 帐户名
  • AccountBalance(返回的交易列总和)
  • 交易(EntitySet的)

我正在为显示交易清单一个给定的帐户,我遇到了一些麻烦。我想要做的是显示按日期分组的交易。为此,我使用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周并对现有交易进行更改)。

编辑我想看到的:

[2011年10月2日-------------- $ 753.23]

事务1 - 杂货 - $ 30.00

[2011年10月1日-------------- $ 723.23]

银行 - 车 - $ 400.00

商店 - 杂货 - $ 35.00

[2011年9月31日-------------- $ 288.23]

回答

1

在我的Transaction<T>类,我添加了名为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绑定到该属性。也许有这样一种更清洁的方式,但我认为重新计算余额比存储数据库中的余额更清洁。