tags:

views:

56

answers:

2

I have a District class that looks like this:

public class District
{
    public int Id { get; set; }
    public string Name { get; set; }
    public District Parent { get; set; }
    public IEnumerable<District> Ancestors { get { /* what goes here? */ } }
}

I would like to be able to get a list of each District's ancestors. So if District "1.1.1" is a child of District "1.1", which is a child of District "1", getting Ancestors on District "1.1.1" would return a list that contains the District objects whose names are "1.1" and "1".

Does this involve the yield return statement (I never totally understood that)? Can it be done in one line?

+6  A: 

Everything can be done in one line, if the line is long enough :)

In this case, it's probably simplest done not in one line:

public IEnumerable<District> Ancestors
{
    get
    {
        District parent = Parent;
        while (parent != null)
        {
            yield return parent;
            parent = parent.Parent;
        }
    }
}

Just a brief plug if you want to understand yield return better - chapter 6 of the first edition of C# in Depth is still available for free, and that's all about iterators in C# 2. Grab it from the first edition page.

Jon Skeet
This looks good - trying it now.
Chris
what about a circular reference?
Elijah Glover
Jon Skeet
There are indeed no circular references, so this works perfectly. I ended up needing to include the current district in the list as well, but your code was very easy to read and modify. Thank you. I'll check out your book, too!
Chris
+1  A: 

You may want to consider this alternative.... its not as "pure" but massively effective way to easily query for this kind of thing. (whether using sql or not)

http://stackoverflow.com/questions/2742242/sql-select-descendants-of-a-row

see the "Accepted" answer

the only thing I'd add is a delimiter between tags

oh and then to query using linq

ancestors = Districts.Where( d => 
       d.Pedigree.Contains(Id) && 
       d.Pedigree.Length < Pedigree)
       .ToList();

for the most if you are using an ORM this will result in a single query rather than many queries when trying to iterate the tree

actually in this case of a single parent, its even easier as your pedigree will contain your ancestry, but if you had a branching ancestry..... :)

Keith Nicholas
Pretty damn clever. Bookmarked!
Chris