private static async Task DeleteChildNodesAsync <TGroup, TItem>(PortalClient portalClient, TGroup group) where TGroup : IdentifiedItem, IHasEndpoint, new() where TItem : IdentifiedItem, IHasEndpoint, new() { IEnumerable <int> ids; switch (group) { case CollectorGroup collectorGroup: ids = (await portalClient .GetAllCollectorsByCollectorGroupId(collectorGroup.Id) .ConfigureAwait(false))?.Select(c => c.Id) ?? Enumerable.Empty <int>(); break; case DashboardGroup dashboardGroup: ids = (await portalClient .GetAllAsync(new Filter <Dashboard> { FilterItems = new List <FilterItem <Dashboard> > { new Eq <Dashboard>(nameof(Dashboard.DashboardGroupId), dashboardGroup.Id) } }) .ConfigureAwait(false))?.Select(r => r.Id) ?? Enumerable.Empty <int>(); break; case DeviceGroup deviceGroup: ids = deviceGroup.Devices?.Select(d => d.Id) ?? Enumerable.Empty <int>(); break; case NetscanGroup netscanGroup: ids = (await portalClient .GetAllAsync(new Filter <Netscan> { FilterItems = new List <FilterItem <Netscan> > { new Eq <Netscan>(nameof(Netscan.GroupId), netscanGroup.Id) } }) .ConfigureAwait(false))?.Select(r => r.Id) ?? Enumerable.Empty <int>(); break; case ReportGroup reportGroup: ids = (await portalClient .GetAllAsync(new Filter <Report> { FilterItems = new List <FilterItem <Report> > { new Eq <Report>(nameof(Report.GroupId), reportGroup.Id) } }) .ConfigureAwait(false))?.Select(r => r.Id) ?? Enumerable.Empty <int>(); break; case WebsiteGroup websiteGroup: ids = (await portalClient .GetAllAsync(new Filter <Website> { FilterItems = new List <FilterItem <Website> > { new Eq <Website>(nameof(Website.WebsiteGroupId), websiteGroup.Id) } }) .ConfigureAwait(false))?.Select(w => w.Id) ?? Enumerable.Empty <int>(); break; default: throw new NotSupportedException($"Deleting '{typeof(TGroup).Name}' child items not supported."); } // We have the list of ids to delete foreach (var id in ids) { await DeleteAsync <TItem>(portalClient, id).ConfigureAwait(false); } }