Click here to Skip to main content
15,891,184 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.93/5 (111 votes)
4 Mar 2010CPOL14 min read 361.7K   3.1K   234  
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.

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqToTreeWPF", "LinqToTreeWPF\LinqToTreeWPF.csproj", "{32B3E05F-5DE3-41C1-AC87-6600968FA36E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CodeGen", "CodeGen", "{26E35061-D099-4D25-A693-2B9082BEB367}"
	ProjectSection(SolutionItems) = preProject
		CodeGen\LinqToTreeCodeGen.tt = CodeGen\LinqToTreeCodeGen.tt
	EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqToTree", "LinqToTree\LinqToTree.csproj", "{DEF16D9D-F22D-4CDE-9BBD-03BA14DC4530}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqToVisualTreeWPF", "CodeGen\LinqToTreeWPF\LinqToVisualTreeWPF.csproj", "{3651FE4E-8CB7-4776-97B0-5E00F867EE05}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqToFileSystem", "CodeGen\LinqToFileSystem\LinqToFileSystem.csproj", "{A1A455CD-D6FE-48F7-B33E-79D3F57009AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinqToWindowsForms", "CodeGen\LinqToWindowsForms\LinqToWindowsForms.csproj", "{28B52083-A17B-4761-9F30-53D1AFC98FA4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7C16F871-49B3-4271-841F-5EFF69FD8711}"
	ProjectSection(SolutionItems) = preProject
		README.txt = README.txt
	EndProjectSection
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{32B3E05F-5DE3-41C1-AC87-6600968FA36E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{32B3E05F-5DE3-41C1-AC87-6600968FA36E}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{32B3E05F-5DE3-41C1-AC87-6600968FA36E}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{32B3E05F-5DE3-41C1-AC87-6600968FA36E}.Release|Any CPU.Build.0 = Release|Any CPU
		{DEF16D9D-F22D-4CDE-9BBD-03BA14DC4530}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{DEF16D9D-F22D-4CDE-9BBD-03BA14DC4530}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{DEF16D9D-F22D-4CDE-9BBD-03BA14DC4530}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{DEF16D9D-F22D-4CDE-9BBD-03BA14DC4530}.Release|Any CPU.Build.0 = Release|Any CPU
		{3651FE4E-8CB7-4776-97B0-5E00F867EE05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{3651FE4E-8CB7-4776-97B0-5E00F867EE05}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{3651FE4E-8CB7-4776-97B0-5E00F867EE05}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{3651FE4E-8CB7-4776-97B0-5E00F867EE05}.Release|Any CPU.Build.0 = Release|Any CPU
		{A1A455CD-D6FE-48F7-B33E-79D3F57009AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{A1A455CD-D6FE-48F7-B33E-79D3F57009AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{A1A455CD-D6FE-48F7-B33E-79D3F57009AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{A1A455CD-D6FE-48F7-B33E-79D3F57009AA}.Release|Any CPU.Build.0 = Release|Any CPU
		{28B52083-A17B-4761-9F30-53D1AFC98FA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{28B52083-A17B-4761-9F30-53D1AFC98FA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{28B52083-A17B-4761-9F30-53D1AFC98FA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{28B52083-A17B-4761-9F30-53D1AFC98FA4}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(NestedProjects) = preSolution
		{3651FE4E-8CB7-4776-97B0-5E00F867EE05} = {26E35061-D099-4D25-A693-2B9082BEB367}
		{A1A455CD-D6FE-48F7-B33E-79D3F57009AA} = {26E35061-D099-4D25-A693-2B9082BEB367}
		{28B52083-A17B-4761-9F30-53D1AFC98FA4} = {26E35061-D099-4D25-A693-2B9082BEB367}
	EndGlobalSection
EndGlobal

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.

License

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


Written By
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

-

Comments and Discussions