<p>I have a <code>DataTable</code> with a <code>Name</code> column. I want to generate a collection of the unique names ordered alphabetically. The following query ignores the <em>order by</em> clause.</p>

<pre><code>var names =
    (from DataRow dr in dataTable.Rows
    orderby (string)dr["Name"]
    select (string)dr["Name"]).Distinct();
</code></pre>

<p>Why does the <code>orderby</code> not get enforced?</p>

[{'answer_id': 62, 'author': 'Bob', 'author_id': 45, 'author_profile': 'https://Stackoverflow.com/users/45', 'pm_score': 6, 'selected': False, 'text': '<p><strong>The problem is that the Distinct\n operator does not grant that it will\n maintain the original order of\n values.</strong></p>\n\n<p>So your query will need to work like this</p>\n\n<pre><code>var names = (from DataRow dr in dataTable.Rows\n             select (string)dr["Name"]).Distinct().OrderBy( name =&gt; name );\n</code></pre>\n'}, {'answer_id': 4313, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 2, 'selected': False, 'text': '<p>Try the following</p>\n\n<pre><code>var names = (from dr in dataTable.Rows\n             select (string)dr["Name"]).Distinct().OrderBy(name =&gt; name);\n</code></pre>\n\n<p>this should work for what you need.</p>\n'}, {'answer_id': 43110, 'author': 'a7drew', 'author_id': 4239, 'author_profile': 'https://Stackoverflow.com/users/4239', 'pm_score': 6, 'selected': True, 'text': "<p>To make it more readable and maintainable, you can also split it up into multiple LINQ statements.</p>\n\n<ol>\n<li>First, select your data into a new list, let's call it <code>x1</code>, do a projection if desired</li>\n<li>Next, create a distinct list, from <code>x1</code> into <code>x2</code>, using whatever distinction you require</li>\n<li>Finally, create an ordered list, from <code>x2</code> into <code>x3</code>, sorting by whatever you desire </li>\n</ol>\n"}, {'answer_id': 342600, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<pre><code>var sortedTable = (from results in resultTable.AsEnumerable()\nselect (string)results[attributeList]).Distinct().OrderBy(name =&gt; name);\n</code></pre>\n'}, {'answer_id': 16261469, 'author': 'Gavin Fang', 'author_id': 2010092, 'author_profile': 'https://Stackoverflow.com/users/2010092', 'pm_score': 3, 'selected': False, 'text': '<p>Try out the following:</p>\n\n<pre><code>dataTable.Rows.Cast&lt;DataRow&gt;().select(dr =&gt; dr["Name"].ToString()).Distinct().OrderBy(name =&gt; name);\n</code></pre>\n'}, {'answer_id': 48526016, 'author': 'Philip Raath', 'author_id': 3919949, 'author_profile': 'https://Stackoverflow.com/users/3919949', 'pm_score': 2, 'selected': False, 'text': '<p>To abstract: all of the answers have something in common.</p>\n\n<p>OrderBy needs to be the final operation.</p>\n'}, {'answer_id': 51021828, 'author': 'Presto', 'author_id': 8479567, 'author_profile': 'https://Stackoverflow.com/users/8479567', 'pm_score': 2, 'selected': False, 'text': '<p>You can use something like that:</p>\n\n<pre><code>dataTable.Rows.Cast&lt;DataRow&gt;().GroupBy(g =&gt; g["Name"]).Select(s =&gt; s.First()).OrderBy(o =&gt; o["Name"]);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/59', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/45/']