public ClientContractChannelViewModel AllClientContractChannels(int clientContractId) { var result = new ClientContractChannelViewModel(); try { result = this.GetClientContractChannels(clientContractId); } catch (Exception ex) { try { result = this.GetClientContractChannels(clientContractId); } catch (Exception ex1) { try { result = this.GetClientContractChannels(clientContractId); } catch (Exception ex2) { return new ClientContractChannelViewModel(); } } } return result; }
private ClientContractChannelViewModel GetClientContractChannels(int clientContractId) { var clientContractProviderId = this.Data.ClientContracts .All() .Where(c => c.Id == clientContractId) .FirstOrDefault(c => c.Id == clientContractId) .ProviderId; var providerChannels = this.Data.Channels .All() .ProjectTo<ChannelViewModel>() .Where(c => c.ProviderId == clientContractProviderId) .ToList(); var clientContract = this.Data.ClientContracts .GetById(clientContractId); var clientContractChannels = new List<ChannelViewModel>(); foreach (var channel in clientContract.Channels) { var channelAsModel = this.Data.Channels .All() .Where(c => c.Id == channel.Id) .ProjectTo<ChannelViewModel>() .FirstOrDefault(); clientContractChannels.Add(channelAsModel); } var indexesOfElementForRemove = new List<int>(); foreach (var channel in clientContractChannels) { indexesOfElementForRemove.AddRange( from providerChannel in providerChannels where providerChannel.Id == channel.Id select providerChannels .FindIndex(p => p.Id == channel.Id)); } var sortedListFromIndexes = indexesOfElementForRemove.OrderByDescending(i => i).ToList(); foreach (var index in sortedListFromIndexes) { providerChannels.RemoveAt(index); } var result = new ClientContractChannelViewModel() { ProviderChannels = providerChannels, ClientContractChannels = clientContractChannels, ClientContractId = clientContractId }; return result; }