title: how to sort varchar column containing numeric values with linq lambdas to Entity
body: <p>I am using linq lambdas to query the MySql (Note MySql not Sql) with Entity Framwork in MVC. Now i have one table <code>product</code> one of column this table is <code>price</code> with datatype "VARCHAR" (Accept i can't change type to INT as it can hold values like "N/A",etc).</p>
  
  <p>I want to sort <code>price</code> column numerically with linq Lambdas.I have tried bellow.I am using <code>Model</code> values to filter query.</p>
  
  <pre><code>var query = ent.Product.Where(b =&gt; b.cp == Model.CodePostal);
  
  if (Model.order_by_flg == 2)
  {
      query = query.OrderByDescending(a =&gt; a.price.PadLeft(10, '0'));
  }
  </code></pre>
  
  <p>But it will not work and give me bellow error.</p>
  
  <blockquote>
    <p>LINQ to Entities does not recognize the method 'System.String
    PadLeft(Int32, Char)' method, and this method cannot be translated
    into a store expression.</p>
  </blockquote>
  
  <p>As it cant convert to Sql statement by Entity Framwork.</p>
  
  <p>I also tried bellow.</p>
  
  <pre><code>var query = ent.Product.Where(b =&gt; b.cp == Model.CodePostal);
  
  if (Model.order_by_flg == 2)
  {
      query = query.OrderByDescending(a =&gt; a.price.Length).ThenBy(a =&gt; a.price);
  }
  </code></pre>
  
  <p>But i can't do this because it works for List but i cant first make list and then do this as i am using linq <code>Skip()</code> and <code>Take()</code> so first i have to sort it.</p>
  
  <p>So how can i sort price column of type "VARCHAR" in Linq lambda?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>In table it is : </p>
  
  <pre><code>59,59,400,185,34
  </code></pre>
  
  <p>Wnen i use OrderBy.ThenBy it gives</p>
  
  <pre><code>34,59,59,106,185,400
  </code></pre>
  
  <p>It looks right as sorting ascending But when i use OrderByDescending.ThenBy it gives</p>
  
  <pre><code>106,185,400,34,59,59
  </code></pre>
  
  <p>So i can't use this.</p>
  
  <p><strong>NOTE: Please give reasons before Downvote so i can improve my question...</strong></p>