How can I select tree nodes independently from their nesting level?
public
class
TreeNode
{
string
Id {
get
;
set
; }
int
Data {
List<TreeNode> Children {
private
TreeNode(
id,
data)
:
this
(id, data,
new
TreeNode[0])
{ }
node,
data,
params
TreeNode[] children)
Id = node;
Data = data;
Children =
List<TreeNode>(children);
}
TreeNode rootNode =
"node-0"
, 35,
"node-1"
, 17,
"node-2"
, 20)),
"node-3"
, 19),
"node-4"
, 5,
"node-5"
, 25),
"node-6"
, 40)));
↑ Return to Top
static
LinqTreeExtension
IEnumerable<T> SelectNestedChildren<T>
(
IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
foreach
(T item
in
source)
yield
return
item;
(T subItem
SelectNestedChildren(selector(item), selector))
subItem;
var flattenedTreeWithoutRoot
= rootNode.Children
.SelectNestedChildren(t => t.Children).ToList();
var flattenedTreeIncludingRoot
=
TreeNode[] {rootNode }
var result
TreeNode[] { rootNode }
.SelectNestedChildren(t => t.Children)
.Where(t => t.Data > 30).ToList();
var databaseResult = context.TreeNodes
var xmlElements = XDocument.Load(
"myfile.xml"
).Descendants();
var result = xmlElements.Where(x => ...);
Carsten Siemens edited Revision 13. Comment: Added section: "Other languages". Added tags: Multi Language Wiki Articles, has Other Languages
Carsten Siemens edited Revision 12. Comment: Fixed spelling - community voted 2:1 for "contradiction" instead of "contraindication"
Carsten Siemens edited Revision 10. Comment: Fixed formatting in section "Links"
Carsten Siemens edited Revision 9. Comment: Reset the title of a chapter to "Contraindications and alternatives" instead of "Contradictions and alternatives"
Naomi N edited Revision 8. Comment: Nice article
Carsten Siemens edited Revision 7. Comment: Added "See Also" section
Carsten Siemens edited Revision 5. Comment: Fixed size of a code box
Carsten Siemens edited Revision 3. Comment: Added tag: "Carsten Siemens"
Carsten Siemens edited Revision 2. Comment: Fixed tag: Best Practice
Carsten Siemens edited Revision 1. Comment: Fixed link "Return to top"
Carsten Siemens edited Original. Comment: Corrected case in title
Carsten Siemens edited Revision 4. Comment: Fixed misspelling
Can you please explain the usage of the word contraindication www.thefreedictionary.com/contraindication in the context of this article?
Hello Naomi,
according to Merriam-Webster - www.merriam-webster.com/.../contraindication - a contraindication is
"something (as a symptom or condition) that makes a particular treatment or procedure inadvisable".
Let’s map this definition to the article:
* The “treatment” is the class LinqTreeExtension which is explained in the “Solution” section.
* A condition which makes this “treatment” inadvisable is for example a cyclic graph.
The term contraindication is most often used by physicians and pharmacists. But (at least in Germany) it is not limited to medicine.
I like a figurative language, but I’m not a native speaker. You may have better synonyms.
Naomi, I really appreciate your careful editing. Thank you!
I am also not a native speaker, so when I saw that word, I had to check it up. It may be nice if an English native speaker will verify if it is indeed permissible to be used in this context. That's why I originally changed it to more often and commonly used contradiction although may be just Pros and Cons may be used
Naomi is correct, it's ConTRADiction. ConTRAINdication is a rarely used medical term.
Simple typo IMO.
"Contradiction" is very much the right word.
Regards,
Pete