private void StatisticUpdated() { try { Logger.Trace("Starting task update accounts.."); var sortedAccounts = accountService.SortAccounts(); if (!sortedAccounts.IsSuccess) { throw new Exception(sortedAccounts.Error); } var currentStatistic = statisticRepository.GetCurrentStatistics(); var allAccounts = sortedAccounts.Result .TradingAccounts.Select(x => new Tuple <AccountRole, RatingAccount>(AccountRole.Trading, x)) .Union(sortedAccounts.Result.SignalProviders.Select(x => new Tuple <AccountRole, RatingAccount>(AccountRole.SignalProvider, x))) .Union(sortedAccounts.Result.Managers.Select(x => new Tuple <AccountRole, RatingAccount>(AccountRole.Master, x))) .ToDictionary(x => x.Item2.AccountId, x => x); var allAccountsStatistic = currentStatistic.Values .Where(x => allAccounts.ContainsKey(x.account_id) && (allAccounts[x.account_id].Item1 == AccountRole.Trading || allAccounts[x.account_id].Item1 == AccountRole.SignalProvider)) .OrderByDescending(x => x.closed_profit_in_points_total) .Union(currentStatistic.Values .Where(x => allAccounts.ContainsKey(x.account_id) && allAccounts[x.account_id].Item1 == AccountRole.Master) .OrderByDescending(x => x.closed_profit_in_percents_total)) .ToDictionary(x => x.account_id, x => x); var traders = new List <Trader>(); var providers = new List <SignalProvider>(); var s = new List <Manager>(); foreach (var account in allAccounts) { stat_statistics s; var lastStatistic = allAccountsStatistic.TryGetValue(account.Key, out s) ? new AccountDayStatistic(s) : new AccountDayStatistic(); AddStatisticAccount(account.Value.Item2, lastStatistic, account.Value.Item1, traders, providers, s); } traders = traders.DistinctBy(x => x.AccountId).ToList(); providers = providers.DistinctBy(x => x.AccountId).ToList(); s = s.DistinctBy(x => x.AccountId).ToList(); lock (topAccountsLock) { topTraders = traders.ToDictionary(x => x.AccountId, x => x); topSignalProviders = providers.ToDictionary(x => x.AccountId, x => x); topManagers = s.ToDictionary(x => x.AccountId, x => x); } Logger.Trace("Updating accounts done"); } catch (Exception ex) { Logger.Error("Error at updating statistic: {0}", ex.ToString()); } }