title: Custom method in IQueryable<T>.Select
body: <p>I need to run custom method in <code>IQueryable&lt;T&gt;.Select</code> statement, e.g.</p>
  
  <pre><code>query.Select(r =&gt; new ResultObject()
  {
  Id = r.Persion.Id,
  Code = r.Person.Code,
  CalculatedField = CalculateField(r.Person)
  }
  </code></pre>
  
  <p>Problem is, I can't run it like this, as linq-to-sql exception is thrown. CalculateField is basically a switch statement:</p>
  
  <pre><code>private static CalcFieldEnum CalculateField(Person p)
  {
      if(p.Age &lt; 21)
      {
           if(p.Salary &gt; 2000 &amp;&amp; p.YearsWorked &gt; 2)
               return CalcFieldEnum.TypeB;
           else if(p.YearsWorked &lt;= 2)
               return CalcFieldEnum.TypeC;
      }
      else
      {
          if(p.Salary &gt; 3000)
              return CalcFieldEnum.TypeB;
      }
  
      return CalcFieldEnum.TypeA;
  }
  </code></pre>
  
  <p>The only thing that matters about the method (CalculateField) is that it just takes some entity and returns result based on entity's field values. </p>
  
  <p>I can't run this method post-query, as user can sort results by columns, and sorting run on IQueryable as well. Is there any way I can solve this?</p>
  
  <p><strong>P.S</strong> I simplified the method's logic, in reality there are <em>a lot</em> more of those if-else statements. The idea is the same, though - it just returns an enum value based on object's fields.</p>