title: Unable to cast the type 'System.Int32' to type 'System.Object during EF Code First Orderby Function
body: <p>I'm using Specification pattern in EF Code First. When I do order by operation, VS throw a new exception </p>
  
  <p><a href="https://i.stack.imgur.com/3v2rB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/3v2rB.png" alt="enter image description here"></a></p>
  
  <p>The specification pattern is copy from <strong>eShopOnWeb</strong></p>
  
  <p>I just change a little bit, here is my change code:</p>
  
  <pre><code>public class Specification&lt;T&gt; : ISpecification&lt;T&gt;
  { 
  
      public Expression&lt;Func&lt;T, object&gt;&gt; OrderBy { get; private set; }
  
      public Specification(Expression&lt;Func&lt;T, bool&gt;&gt; criteria)
      {
          Criteria = criteria;
      }
  
      public Specification&lt;T&gt; OrderByFunc(Expression&lt;Func&lt;T, object&gt;&gt; orderByExpression)
      {
          OrderBy = orderByExpression;
          return this;
      }
  }
  </code></pre>
  
  <p>Here is my invoke code, it's very pretty simple:</p>
  
  <pre><code>static void TestSpec()
      {
          var spec = new Specification&lt;ExcelData&gt;(x =&gt; x.RowIndex == 5)
              .OrderByFunc(x =&gt; x.ColumnIndex);
  
          using (var dbContext = new TechDbContext())
          {
              var top10Data = dbContext.ExcelData.Take(10).ToList();
              var listExcel = dbContext.ApplySpecification(spec).ToList();
  
              Console.WriteLine();
          }
      }
  </code></pre>
  
  <p>If I comment <strong>OrderByFunc</strong>, then everything is fine to me. no error throw from vs.</p>
  
  <p>I had try many times search the error message in google, but none of answer is my case. </p>
  
  <p>So I have to ask a question in here.</p>
  
  <p>When I debug <strong>OrderBy</strong> property in <a href="https://github.com/dotnet-architecture/eShopOnWeb/blob/master/src/Infrastructure/Data/SpecificationEvaluator.cs" rel="noreferrer">SpecificationEvaluator.cs</a>, I found there is a <strong>Convert</strong> method.
  <a href="https://i.stack.imgur.com/w7RGr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/w7RGr.png" alt="enter image description here"></a></p>
  
  <p>So I know the error is about cast error, but how do I fix this cast type error?</p>
  
  <p>Please help me!</p>