Click here to Skip to main content
12,631,574 members (33,844 online)
Click here to Skip to main content


236 bookmarked

LINQ to Tree - A Generic Technique for Querying Tree-like Structures

, 4 Mar 2010 CPOL
This article presents a generic approach to applying LINQ queries to tree like structures. Using T4 templates for code generation, LINQ to VisualTree (WPF), LINQ to WinForms, and LINQ to FileSystem APIs are constructed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using LinqToVisualTree;
using System.Diagnostics;
using System.Xml.Linq;

namespace LinqToTreeWPF
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
        public Window1()

        private void Window_ContentRendered(object sender, EventArgs e)

            // get all the TextBox's which have a Grid as direct parent
            var itemsFluent = this.Descendants<TextBox>()
                                  .Where(i => i.Ancestors().FirstOrDefault() is Grid);

            var itemsQuery = from v in this.Descendants<TextBox>()
                             where v.Ancestors().FirstOrDefault() is Grid
                             select v;

            // get all the StackPanels that are within another StackPanel visual tree
            var items2Fluent = this.Descendants<StackPanel>()

            var items2Query = from i in
                                 (from v in this.Descendants<StackPanel>()                                   
                                  select v).Descendants<StackPanel>()
                              select i;

            // output a tree!
            string tree = this.DescendantsAndSelf().Aggregate("",
                (bc, n) => bc + n.Ancestors().Aggregate("", (ac, m) => (m.ElementsAfterSelf().Any() ? "| " : "  ") + ac,
                ac => ac + (n.ElementsAfterSelf().Any() ? "+-" : "\\-")) + n.GetType().Name + "\n");


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Colin Eberhardt
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt


You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 5 Mar 2010
Article Copyright 2010 by Colin Eberhardt
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid