Metainformationen zur Seite
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
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=" | <code C# [enable_line_numbers=" | ||
+ | |||
IEnumerable< | IEnumerable< | ||
</ | </ | ||
Zeile 114: | Zeile 115: | ||
For breadth-first collections, | For breadth-first collections, | ||
<code C# [enable_line_numbers=" | <code C# [enable_line_numbers=" | ||
+ | |||
IEnumerable< | IEnumerable< | ||
</ | </ | ||
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, | 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, | ||
- | 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:// | ||