开发者

Tracking Account balance and information

开发者 https://www.devze.com 2023-04-10 02:04 出处:网络
I\'m working on a simple financial application using MVVM & LinqToSql. I\'m trying to find out the best way to structure my data. Here\'s what I have (simplified):

I'm working on a simple financial application using MVVM & LinqToSql. I'm trying to find out the best way to structure my data. Here's what I have (simplified):

Account

  • AccountId (PK)
  • AccountName
  • AccountBalance (returns a sum on the transactions column)
  • Transactions (EntitySet)

I'm working on displaying the list of transactions for a given account, and I'm running into some trouble. What I'd like to do is display the transactions grouped by their date. To do this, I'm using the LongListSelector control, which allows for grouping. The datasource for the control is the following:

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;

This works, and I see my group titles with the date, with transaction data for that day underneath it.

What I'm having an issue with is displaying the daily balance in the header with each date. How can I structure my data so that the account balance is easily accessible by date, but开发者_开发问答 can be updated (if the user goes 2 weeks back and makes a change to an existing transaction).

Edit What I'd like to see:

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

Transaction 1 - Grocery - $30.00

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

Bank - Vehicle - $400.00

Store - Grocery - $35.00

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

etc


Within my Transaction<T> class, I added another property called TransAmount. So when the constructor is called from the code block above, here's what it's doing.

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);
}

Then I just bind my XAML to that property. Perhaps there is a cleaner way of doing this, but I think re-calculating the balance would be cleaner than storing the balance int he database.

0

精彩评论

暂无评论...
验证码 换一张
取 消