title: Entity Framework Sum DateTime
body: <p>I have the following query that calculates a total duration. The total duration is subtraction between StartingDate and EndingDate (StartingDate - EndingDate):</p>
  
  <pre><code>var v = from m in db.PageRequests
          group m by m.SiteSession into g
          select new
          {
              totalDuration = g.Key.PageRequests.Where(x =&gt; x.EndingDate.HasValue).Sum(x =&gt; x.EndingDate.Value.Ticks - x.StartingDate.Value.Ticks)
          };
  </code></pre>
  
  <p>As the Sum function returns a number (int) I thought to use DateTime.<strong>Ticks</strong> but then it thrown the following exception:</p>
  
  <blockquote>
    <p>The specified type member 'Ticks' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.</p>
  </blockquote>
  
  <p>I saw some solutions with <code>EntityFunctions</code> and <code>DbFunctions</code> but they work on a specific time scope (days, minutes etc). I need it in any time scope (might be milliseconds, seconds, hours, days etc).</p>