Taxon.Taxa?

Posted by Community Admin on 04-Aug-2018 19:16

Taxon.Taxa?

All Replies

Posted by Community Admin on 04-Apr-2012 00:00

Why is there no Children property for a taxon object?  Am I just missing it, or a using clause?

I need to be at a taxon, and flatten it and its children out to a List<Taxon>

Posted by Community Admin on 05-Apr-2012 00:00

By children are you not referring to Taxon.Taxa? because I believe that Taxa is the property for all child taxonomy types for a given taxon

Posted by Community Admin on 05-Apr-2012 00:00

Well HierachicalTaxonomy has a Taxa property, which i'm using to get the Taxon I want as my "Root"

var categories = manager.GetTaxonomies<HierarchicalTaxonomy>().SingleOrDefault(x => x.Title == "Categories");

var mfTaxa = categories.Taxa.SingleOrDefault(x => x.Title == "MF" + mfNumber && x.Parent == null);

So now that I have that guy...I don't see a "Taxa" property on "mfTaxa".  It has parents properties, but no child properties (that I can see).

Posted by Community Admin on 05-Apr-2012 00:00

aha I gotcha now, and you're right there should be something in there to get a hierarchicaltaxon's taxa.

Looking at the HierarchicalTaxon type in JustDecompile it appears there is a virtual property Subtaxa that appears to return what you want, but I can't figure out how to actually get access to it...

I've sent an email to the Sitefinity devs for more info on this property and if indeed this is even the way to get at it, I will report back as soon as I've got something for you, thanks for the feedback!

EDIT: in the meantime it occurs to me that you could use the taxonManager to grab an instance of the HiearchicalTaxonomy type instead, using that HiearachicalTaxon.Id. It's the long way around but might let you move forward until I hear back from the team.

hope this is helpful!

Posted by Community Admin on 05-Apr-2012 00:00

AH JOSH!  You struck the nail on the head...It needed a cast to HierarchicalTaxon!

var subunits = ((HierarchicalTaxon)mfTaxa).Subtaxa;

Thanks much...the documentation and summary notes make it seem like the Taxon object itself is both flat and Hierachical since it has a Parent property.

Posted by Community Admin on 05-Apr-2012 00:00

So knowing that now...I'd love it if you guys just PLOPPED this into the core SF code :)  This is super handy as an extension...

public static class TaxonExtensions
    public static IEnumerable<HierarchicalTaxon> FlattenHierarchy(this HierarchicalTaxon parent)
    
        foreach (HierarchicalTaxon control in parent.Subtaxa)
        
            yield return control;
            foreach (HierarchicalTaxon descendant in control.FlattenHierarchy())
            
                yield return descendant;
            
        
    

Posted by Community Admin on 05-Apr-2012 00:00

I'm glad you got it worked out! strangely enough, it occred to me to suggest that you cast the Taxon to HierarchicalTaxon but I kept getting compile errors that the cast was invalid.

Turns out I was accidentally trying to cast to HiearchicalTaxonomy instead of Taxon, but you figured that out anyway :)

Posted by Community Admin on 05-Apr-2012 00:00

Did you try the extension method?

Posted by Community Admin on 06-Apr-2012 00:00

Doesn't even need to BE an extension method actually...but if it could be included that would be awesome since it helps facilitate .Contains Linq queries.  We're just being totally ignored on the topic :)

(not by you clearly)

This thread is closed