Building a linked list with LINQ
Posted
by
FreshCode
on Stack Overflow
See other posts from Stack Overflow
or by FreshCode
Published on 2011-01-09T18:58:54Z
Indexed on
2011/01/09
19:53 UTC
Read the original article
Hit count: 184
What is the fastest way to order an unordered list of elements by predecessor (or parent) element index using LINQ?
Each element has a unique ID and the ID of that element's predecessor (or parent) element, from which a linked list can be built to represent an ordered state.
Example
ID | Predecessor's ID
--------|--------------------
20 | 81
81 | NULL
65 | 12
12 | 20
120 | 65
The sorted order is {81, 20, 12, 65, 120}. An (ordered) linked list can easily be assembled iteratively from these elements, but can it be done in fewer LINQ statements?
Edit: I should have specified that IDs are not necessarily sequential. I chose 1 to 5 for simplicity. See updated element indices which are random.
© Stack Overflow or respective owner