2011-10-03 121 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綁定到該屬性。也許有這樣一種更清潔的方式,但我認爲重新計算餘額比存儲數據庫中的餘額更清潔。