Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This option will give you a sorted <code>IOrderedEnumerable</code>, with the xml for each user, and the managerId separately. Variations of this should give you a lot of flexibility:</p> <pre><code>var sortedUsers = yourRootXElement.Element("users") .Elements() .Select(usr =&gt; new { managerId = usr.Element("manager") .Element("dn") .Value, user = usr }) .OrderBy(user =&gt; user.managerId); </code></pre> <p><strong>Update:</strong></p> <p>Based on a comment under one of the other answers, it seems you want to reorder this "recursively" (sort of). </p> <p>Initially, I don't that is very easy using Linq - Unless you have the option of manipulating your original XML, you might be better of writing a recursive algorithm for this. </p> <p>If however, you can add a field <code>depth</code> to each user in your XML, so that it would look something like this..: </p> <pre><code>&lt;syncdata&gt; &lt;users&gt; &lt;user&gt; &lt;dn&gt;User2&lt;/dn&gt; &lt;manager&gt;&lt;dn&gt;User1&lt;/dn&gt;&lt;/manager&gt; &lt;depth&gt;2&lt;/depth&gt; &lt;/user&gt; &lt;user&gt; &lt;dn&gt;User1&lt;/dn&gt; &lt;manager&gt;&lt;dn&gt;User5&lt;/dn&gt;&lt;/manager&gt; &lt;depth&gt;1&lt;/depth&gt; &lt;/user&gt; &lt;user&gt; &lt;dn&gt;User4&lt;/dn&gt; &lt;manager&gt;&lt;dn&gt;User1&lt;/dn&gt;&lt;/manager&gt; &lt;depth&gt;2&lt;/depth&gt; &lt;/user&gt; &lt;user&gt; &lt;dn&gt;User5&lt;/dn&gt; &lt;manager&gt;&lt;dn&gt;User3&lt;/dn&gt;&lt;/manager&gt; &lt;depth&gt;1&lt;/depth&gt; &lt;/user&gt; &lt;user&gt; &lt;dn&gt;User3&lt;/dn&gt; &lt;manager&gt;&lt;dn&gt;&lt;/dn&gt;&lt;/manager&gt; &lt;depth&gt;0&lt;/depth&gt; &lt;!-- Note: Root element in hierarchy! --&gt; &lt;/user&gt; &lt;/users&gt; &lt;/syncdata&gt; </code></pre> <p>...then you could perhaps order it as follows?:</p> <pre><code>var sortedUsers = yourRootXElement.Element("users") .Elements() .Select(usr =&gt; new { userId = usr.Element("dn").Value, managerId = usr.Element("manager") .Element("dn") .Value, depth = usr.Element("depth") .Value, userData = usr }) .OrderBy(user =&gt; user.depth) .ThenBy(user =&gt; user.managerId); </code></pre> <p><strong>Note:</strong> This should order them by their "reporting-levels" in the hierarchy, but will not show those under a manager listed under him - ie.: All those reporting to a top-level managers will be shown first, followed by all those reporting to a second-level managers, etc... </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload