1

I have an object which has a stucture like this :

    public class CustomObject
    {
        public int CustomProperty{get;set;}
        public string CustomStringProperty{get;set;}
        public List<CustomObject> CustomObjects{get;set;}
    }

I know there are lot of way where I can pull up any object, its parent object or getting an object based on the some condition on the CustomProperty.

I want to write some extension Methods or linq so that Client may have access to each object and its properties.

Currently Client has list --> List<CustomObject>();

eg. ClientMethod(List<CustomObject> customObjs);

Crispin
  • 25
  • 3
  • I'm not sure what you're looking for but I have a guess. Try the this indexer: https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx – Mitulát báti Mar 30 '16 at 12:46
  • Not a direct answer to your question, but you can take a look at https://dvanderboom.wordpress.com/2010/04/05/four-traversal-patterns-for-treet/ – M. Mennan Kara Mar 30 '16 at 13:03

1 Answers1

2

Assuming that you want to be able to traverse the items in the list and all their children.

As usual with such questions, I would suggest you to get the highly efficient general tree preorder traversal method from How to flatten tree via LINQ?:

public static partial class TreeHelper
{
    public static IEnumerable<T> Expand<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> elementSelector)
    {
        var stack = new Stack<IEnumerator<T>>();
        var e = source.GetEnumerator();
        try
        {
            while (true)
            {
                while (e.MoveNext())
                {
                    var item = e.Current;
                    yield return item;
                    var elements = elementSelector(item);
                    if (elements == null) continue;
                    stack.Push(e);
                    e = elements.GetEnumerator();
                }
                if (stack.Count == 0) break;
                e.Dispose();
                e = stack.Pop();
            }
        }
        finally
        {
            e.Dispose();
            while (stack.Count != 0) stack.Pop().Dispose();
        }
    }
}

and utilize it for your needs (you can use a different name if you don't like it):

partial class TreeHelper
{
    public static IEnumerable<CustomObject> Expand(this IEnumerable<CustomObject> source)
    {
        return source.Expand(item => item.CustomObjects != null && item.CustomObjects.Count > 0 ? item.CustomObjects : null);
    }
}

Then you can use simply:

void ClientMethod(List<CustomObject> customObjs)
{
    foreach (var item in customObjs.Expand())
    {
        // do something
    }
}

or LINQ queries like customObjs.Expand().Where(...) etc.

Community
  • 1
  • 1
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • This is excellent thank you. Is there a way to have a property in CustomObject which value tells which nested level a given CustomObjects is in the "tree branch" ? – Kynao Sep 15 '18 at 19:57
  • @Kynao The [linked answer](https://stackoverflow.com/questions/11830174/how-to-flatten-tree-via-linq/31881243#31881243) contains update for providing a level. – Ivan Stoev Sep 15 '18 at 20:39
  • yes in between i saw this other thread and i read and tried for the last hour to understand or at least integrate it here but i don't manage to achieve it. Would you have the same simple 3 blocks code as we see above as even a beginner liek me manage to use these blocks. – Kynao Sep 15 '18 at 20:57