More in this section

Taxon.Taxa?

9 posts, 1 answered
  1. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    04 Apr 2012
    Link to this post
    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>
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    05 Apr 2012
    Link to this post
    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
  3. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    05 Apr 2012
    Link to this post
    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).
  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    05 Apr 2012
    Link to this post
    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!
    Answered
  5. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    05 Apr 2012
    Link to this post
    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.
  6. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    05 Apr 2012
    Link to this post
    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;
                }
            }
        }
    }
  7. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    05 Apr 2012
    Link to this post
    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 :)
  8. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    05 Apr 2012
    Link to this post
    Did you try the extension method?
  9. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    06 Apr 2012
    Link to this post
    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)
9 posts, 1 answered