public static FacetSection getChildSectionRecursive(filter child, string fh_location, IEnumerable<filter> childFilters)
 {
     FacetSection childSection = new FacetSection();
     childSection.Facets = new List<FacetItem>();
     childSection.SectionTitle = child.title;
     childSection.CustomFields = child.customfields;
     List<filtersection> childFilterSections = child.filtersection.ToList();
     foreach (var cfs in childFilterSections)
     {
         FacetItem childFacetItem = getFacetItem(cfs, fh_location, child);
         if (childFacetItem != null)
         {
             childFacetItem.Selected = cfs.selected;
             var rChild = childFilters.Where(s => s.on == cfs.value.Value).FirstOrDefault();
             if (rChild != null)
             {
                 childFacetItem.childSection = getChildSectionRecursive(rChild, fh_location, childFilters);
             }
             childSection.Facets.Add(childFacetItem);
         }
     }
     return childSection;
 }
        public static FacetCollection GetHierarchicalFacetCollection(this facetmap fm, string fh_location, int publicationId)
        {
            FacetCollection facets = new FacetCollection();
            if (fm.filter == null)
            {
                facets.FacetSections = new List<FacetSection>();
                facets.ResetFacetsUrl = Resetfilters(fh_location, false, publicationId).toString();
                return facets;
            }

            List<filter> filters = fm.filter.ToList();
            var uniqueFilters = filters.GroupBy(f => f.title).Select(grp => grp.First());
            IEnumerable<filter> childFilters = uniqueFilters.Where(f => f.on.Contains(FacetedContentHelper.NestedLevelDelimiter));
            IEnumerable<filter> parentFilters = uniqueFilters.Where(f => !f.on.Contains(FacetedContentHelper.NestedLevelDelimiter));

            foreach (filter filter in parentFilters)
            {
                if (((filter.customfields == null) || (filter.customfields.SingleOrDefault<customfield>(c => (c.name == "VisibleFacet")).Value != "False")) || (filter.on == string.Format(WebConfiguration.Current.ProductGroupFormat, WebConfiguration.Current.PublicationId)))
                {
                    FacetSection section = new FacetSection {
                        on = filter.on,
                        Visible = false
                    };
                    if (filter.customfields != null)
                    {
                        section.Visible = filter.customfields.SingleOrDefault<customfield>(c => (c.name == "VisibleFacet")).Value == "True";
                    }
                    section.SectionTitle = filter.title;
                    section.CustomFields = filter.customfields;
                    section.Facets = new List<FacetItem>();
                    List<filtersection> filterSections = filter.filtersection.ToList<filtersection>();
                    foreach (var fs in filterSections)
                    {
                        FacetItem facetItem = getFacetItem(fs, fh_location, filter);
                        var child = childFilters.Where(s => s.on == fs.value.Value).FirstOrDefault();
                        if (child != null)
                        {
                            facetItem.childSection = getChildSectionRecursive(child, fh_location, childFilters);
                        }
                        section.Facets.Add(facetItem);
                    }
                    if(facets.FacetSections == null)
                    {
                        facets.FacetSections = new List<FacetSection>();
                    }
                    facets.FacetSections.Add(section);
                }
            }
            facets.ResetFacetsUrl = Resetfilters(fh_location, true, publicationId).toString();
            return facets;
        }