title: LINQ queries always sort by primary key value even when orderby clause explicitly calls to sort with a different value
body: <p>I am working on my first personal C# project and am incorporating LINQ to make queries against a SQL Server database. I am experiencing an issue with LINQ where queries always sort the records returned from the table according to their primary key value even though <em>orderby</em> clauses exist in the query.</p>
  <p>My objective is to query a table that includes columns for first name and last name (both of type <em>varchar</em>); sort by first name; then sort by last name. The problem code consists of two sections. The first is the original query:</p>
  <pre><code>var dayShiftCrew =
              from information in context.EmployeeInformations
              where information.EmployeeShiftType.Shift == &quot;Day&quot; &amp;&amp; information.EmployeePositionType.Position == &quot;Crew&quot;
              orderby information.EmployeeNamesAndNumber.FirstName, information.EmployeeNamesAndNumber.LastName
              select information.EmployeeNamesAndNumber;
  </code></pre>
  <p>The second part is a <em>foreach</em> statement that populates another table with the query results:</p>
  <pre><code>foreach (var employee in dayShiftCrew)
              {
                  var position =
                      from selection in context.EmployeeInformations
                      where selection.EmployeeID == employee.EmployeeID
                      select selection.Position;
  
                  string query2 = $@&quot;USE Employees; INSERT INTO WeeklySchedule_{month}_{day}_{year} &quot; +
                      @&quot;(EmployeeID, Name, Position) VALUES &quot; +
                      $@&quot;({employee.EmployeeID}, '{employee.FirstName + (&quot; &quot;) + employee.LastName}', '{position.First()}');&quot;;
  
                  SqlCommand command2 = new SqlCommand(query2, connection);
                  command2.ExecuteNonQuery();
              }
  </code></pre>
  <p>When I view the destination table after it is populated, the records are not arranged according to the query's <em>orderby</em> clause. I have executed variations of this code multiple times, and each time the records appear in the same sequence. The sequence in the destination table matches the sequence in the original table, which leads me to think that either the query is sorting by the records' primary key value <em>or</em> is not sorting at all and simply returning the records in their original sequence.</p>
  <p>I'm sure that there is something that I'm overlooking, but I have been experimenting for a while with this code and cannot find a solution to sort correctly. I have tried both kinds of syntax for LINQ queries; tried using one <em>orderby</em> rule instead of two; and rewrote lines for immediate instead of deferred execution.</p>
  <p>Other specific details: The <em>from</em> clause refers to a table with a composite primary key that includes four foreign-key relationships. The query returns an <em>IQueryable</em> that contains the desired records, albeit in the wrong order. No exceptions are thrown at compile or runtime, and no other unexpected behavior is occurring apart from this.</p>