title: LINQ to Entities: Age calculation in a LINQ query causes "Method cannot be translated into a store expression"
body: <p>Using LINQ to Entities, I need to get records from a PersonTable WHERE Age is between AgesFrom and AgesTo. The problem is, I need to calculate their Age from a DateOfBirth and a specific DateToCalculate, both stored in the database.</p>
  
  <p>eg.
  PersonTable: DateOfBirth=01/04/1972, DateToCalculateAge=25/12/2011</p>
  
  <pre><code>.Where(0 &lt; o.FormDate.AddYears(-agesFrom).CompareTo(o.FormDate.Subtract(o.Person.DateOfBirth)) &amp;&amp;
         0 &gt; o.FormDate.AddYears(-agesTo).CompareTo(o.FormDate.Subtract(o.Person.DateOfBirth));
  // ----------------------- OR ------------------------
  .Where(agesFrom &lt;= CalculateAge(o.Person.DateOfBirth.Value, o.FormDate) &amp;&amp;
         agesTo &gt;= CalculateAge(o.Person.DateOfBirth.Value, o.FormDate);
  </code></pre>
  
  <p>For my provided code, I get the following Exception: "LINQ to Entities does not recognize the method 'System.DateTime AddYears(Int32)' method, and this method cannot be translated into a store expression."</p>
  
  <p>I understand that once LINQ passes the Query over to the db, the method(s) I were trying to use could not be translated. Is there a work around for this? I could filter after hitting the database, but want to avoid that sort of bottle neck. I also thought of adding a sql function to do this calculation for me, but want to exhaust the LINQ possibilities first.</p>
  
  <p>A nice post here details how it is better to do the age calculation before the query:
  <a href="https://stackoverflow.com/questions/4715155/shortest-way-to-check-for-age-filter-in-linq">Shortest way to check for age filter in LINQ?</a> However in my situation, I am not calculating age from DateTime.Now, but rather age from a date in the db.</p>