public async Task SyncCrmActivity() { List <CrmAccount> accountsToUpdate = _crmService.GetActiveAccounts(); foreach (CrmAccount account in accountsToUpdate) { DateTime lastActivityDate = new DateTime(0001, 1, 1); var lastAccountActivity = account.FinanceActivities.OrderByDescending(c => c.ContactDate).FirstOrDefault(); if (lastAccountActivity != null) { lastActivityDate = lastAccountActivity.ContactDate; } AtcResponse response = await _anytimeCollectService.GetActivitiesByAccount(account.AtcAccountNumber, lastActivityDate); if (response.Success) { _log.Info($"Found {response.Activities.Count} activities for {account.Name}"); foreach (AtcActivity activity in response.Activities) { var isActivityAlreadySaved = account.FinanceActivities.Where(x => x.AtcActivityId == activity.ActivityId).FirstOrDefault(); if (isActivityAlreadySaved == null) { _crmService.CreateNewActivity(account.Id, activity); } } } } }