title: Can linq2db create query that has ORDER BY by specific column but without including that column in SELECT?
body: <p>I am using linq2db as ORM for my project (ASP.NET Core 2.2) with Oracle database. I want to get list of products from database ordered by a specific column but this column doesn't need to be shown on the front-end client.</p>
  
  <p>SQL query that I am looking for should be something like this:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT Id, Name 
  FROM Products
  ORDER BY InternalOrder
  </code></pre>
  
  <p>So when I use linq2db in my C# code like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using (var db = new ProductsDao())
  {
      return db.Products.Select(p =&gt; new Product
          {
               Id = p.Id,
               MarketName = p.MarketName
          })
          .OrderBy(p =&gt; p.InternalOrder)
          .ToArray();
  }
  </code></pre>
  
  <p>then linq2db converts this to the following SQL query:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT
      t1.Id,
      t1.Name
  FROM
      Products t1
  ORDER BY
      NULL
  </code></pre>
  
  <p>Notice the "ORDER BY NULL" which effectively won't do any ordering.</p>
  
  <p>However if I add InternalOrder in the Select() like this:
  </p>
  
  <pre><code>using (var db = new ProductsDao())
  {
      return db.Products.Select(p =&gt; new Product
          {
               Id = p.Id,
               MarketName = p.MarketName,
               InternalOrder = p.InternalOrder  // this is added
          })
          .OrderBy(p =&gt; p.InternalOrder)
          .ToArray();
  }
  </code></pre>
  
  <p>then I get correct ORDER BY clause in the generated SQL query:
  </p>
  
  <pre><code>SELECT
      t1.Id,
      t1.Name,
      t1.InternalOrder
  FROM
      Products t1
  ORDER BY
      t1.InternalOrder
  </code></pre>
  
  <p>However, in this case I had to include InternalOrder column in the SELECT statement which is something I want to avoid because I don't need that field in the results.</p>
  
  <p>Note, in the real application I might have very big tables and with multiple columns that I want to sort by but don't want to include in the result. Not getting values from database for these columns should save me some performance costs.</p>
  
  <p>For code completeness here are my linq2db related classes that I used in the example above:
  </p>
  
  <pre><code>[Table(Name = "Products")]
  public class Product
  {
      [Column(Name = "Id")] 
      public decimal Id { get; set; }
  
      [Column(Name = "Name")] 
      public string Name { get; set; }
  
      [Column(Name = "InternalOrder", CanBeNull = false)] 
      public int InternalOrder { get; set; }
  }       
  
  public class LiveEventServiceDao : LinqToDB.Data.DataConnection
  {
      public LiveEventServiceDao() : base("MyOracleDb")
      {
      } 
      public ITable&lt;Product&gt; Products =&gt; GetTable&lt;Product&gt;();
  }
  </code></pre>
  
  <p>Is there a way to accomplish this with linq2db?</p>