/// <summary> /// Gets the type request counts (TotalCount and AssignedToYouCount) for the /// given connection types. The Person the service was initialized with is used to calculate /// <see cref="ConnectionRequestCountsViewModel.AssignedToYouCount"/>. /// </summary> /// <remarks>This method does not check security, it is assumed you have already done so.</remarks> /// <param name="connectionTypeIds">The connection type identifiers.</param> /// <returns>A dictionary of connection request count objects.</returns> public Dictionary <int, ConnectionRequestCountsViewModel> GetConnectionTypeCounts(IEnumerable <int> connectionTypeIds) { var opportunityClientService = new ConnectionOpportunityClientService(RockContext, Person); var opportunities = new ConnectionOpportunityService(RockContext) .Queryable() .Where(o => connectionTypeIds.Contains(o.ConnectionTypeId)) .Select(o => new { o.Id, o.ConnectionTypeId }) .ToList(); var opportunityIds = opportunities.Select(o => o.Id).ToList(); var requestCounts = opportunityClientService.GetOpportunityRequestCounts(opportunityIds) .Select(c => new { TypeId = opportunities.Single(o => o.Id == c.Key).ConnectionTypeId, Counts = c.Value }) .GroupBy(c => c.TypeId) .ToDictionary(g => g.Key, g => new ConnectionRequestCountsViewModel { AssignedToYouCount = g.Sum(c => c.Counts.AssignedToYouCount), TotalCount = g.Sum(c => c.Counts.TotalCount) }); // Fill in any missing types with empty counts. foreach (var typeId in connectionTypeIds) { if (!requestCounts.ContainsKey(typeId)) { requestCounts.Add(typeId, new ConnectionRequestCountsViewModel()); } } return(requestCounts); }