I having treeView loaded with custom class collection(eg: MyClass). I want return collection of MyClass[] from the checked tree view items and want use LINQ. I try below and it work fine. But i want to write 1 line Linq query without even using the List<>. Any help ? Is it possible to have recursion within the 1 Linq query ?
List<MyClass> items = new List<MyClass>();
items.AddRange(from node in tvData.Nodes.OfType<TreeNode>().Where((x) => x.Checked)
select node.Tag as MyClass);
tvData.Nodes.OfType<TreeNode>()
.ForEach((x => items.AddRange(from item in x.Nodes.OfType<TreeNode>()
.Where((y) => y.Checked)
select item.Tag as MyClass)));
return items.ToArray();
Note : The treeView having 1 level depth. Every parent node have a set of child nodes and only 1 level.
Try this:
A solution for more than one level could look like this: