Metainformationen zur Seite
  •  

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
start:visualstudio2017:programmieren:dotnetgrundlagen:tipps_tricks:searchingtreeofobjectswithlinq [2022/06/22 10:13]
wikiadmin [Tree to IEnumerable<T> Extension methods]
start:visualstudio2017:programmieren:dotnetgrundlagen:tipps_tricks:searchingtreeofobjectswithlinq [2022/06/22 10:31] (aktuell)
wikiadmin [Tree to IEnumerable<T> Extension methods]
Zeile 87: Zeile 87:
 The calling convention for a depth-first collection is: The calling convention for a depth-first collection is:
 <code C# [enable_line_numbers="true",highlight_lines_extra="0"]> <code C# [enable_line_numbers="true",highlight_lines_extra="0"]>
 +
 IEnumerable<Node> = node.AsDepthFirstEnumerable(n => n.Children);   IEnumerable<Node> = node.AsDepthFirstEnumerable(n => n.Children);  
 </code> </code>
Zeile 114: Zeile 115:
 For breadth-first collections, the calling convention is: For breadth-first collections, the calling convention is:
 <code C# [enable_line_numbers="true",highlight_lines_extra="0"]> <code C# [enable_line_numbers="true",highlight_lines_extra="0"]>
 +
 IEnumerable<Node> = node.AsBreadthFirstEnumerable(n => n.Children);   IEnumerable<Node> = node.AsBreadthFirstEnumerable(n => n.Children);  
 </code> </code>
Zeile 144: Zeile 146:
 The tree used in the example is of course extremely simple, i.e. it doesn’t even have any worthwhile data to query attached to a node. But these extension methods could be used on a node of any kind of tree, **allowing the full power of Linq, grouping, aggregation, sorting, projection, etc. to be used on the tree**. The tree used in the example is of course extremely simple, i.e. it doesn’t even have any worthwhile data to query attached to a node. But these extension methods could be used on a node of any kind of tree, **allowing the full power of Linq, grouping, aggregation, sorting, projection, etc. to be used on the tree**.
  
-As a final note, you may wonder, **why bother with depth-first vs. breadth first?** After all, in the end we do examine every node! There is however one particular case where the choice of algorithm can be very important: **You are looking for one match or a particular number of matches**. Since we are using yield, we can terminate the traversal at any time. Using the FirstOrDefault() extension on our Linq expression, the traversal would stop as soon as one match is found. And if have any knowledge where that node might be in the tree, the choice of search algorithm can be a significant performance factor.+As a final note, you may wonder, **why bother with depth-first vs. breadth first?** After all, in the end we do examine every node! There is however one particular case where the choice of algorithm can be very important: **You are looking for one match or a particular number of matches**. Since we are using yield, we can terminate the traversal at any time. Using the **FirstOrDefault()** extension on our Linq expression, the traversal would stop as soon as one match is found. And if have any knowledge where that node might be in the tree, the choice of search algorithm can be a significant performance factor.
  
 +[[http://http://www.claassen.net/geek/blog/2009/06/searching-tree-of-objects-with-linq.html|Link to the original post from ILogable ]]