using System; using System.Collections.Generic; using System.Linq; using System.Text; using sharpknife.Data; namespace sharpknife.Utils { public class BalanceUtils { public static decimal GetAverageEarning(PTCSource source) { using (databaseEntities context = new databaseEntities()) { var dailyBalance = ( from p in context.BalanceLog where p.Source == source.Name && p.Type == "Balance" orderby p.Date descending group p by p.Date into g select new { Date = g.Key, Value = g.Sum(y => y.Value) } ); var balanceList = (from b in dailyBalance orderby b.Date select b); Balance previousBalance = null; List list = new List(); decimal average = 0m; int count = 0; foreach (dynamic balance in balanceList) { decimal change = 0m; decimal trend = 0m; DateTime date = balance.Date; decimal payouts = ( from p in context.BalanceLog where p.Source == source.Name && p.Type == "Payout" && p.Date < date select (decimal?)p.Value).Sum() ?? 0; if (previousBalance != null) { change = (balance.Value + payouts - previousBalance.Total); trend = change - previousBalance.Change; count++; } average += change; Balance currentBalance = new Balance() { Date = balance.Date, Value = balance.Value, Change = change, ChangeString = change >= 0 ? string.Format("+{0}", change) : change.ToString(), Payout = payouts, Total = balance.Value + payouts, Trend = trend }; list.Add(currentBalance); previousBalance = currentBalance; } if (count > 0) { average = average / count; } return average; } } //public static decimal GetTotalBalance(PTCSource source) //{ //} } }