× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Why can't I project a `TimeSpan` across RIA using `TimeSpan.FromMinutes`?

Gayot Fow
Feb 05, 2015
<p>The issue here is that the CLR TimeSpan class does not have full canonical mapping in LINQ to Entities. This is what your exception is referring to in the clause '...cannot be translated into a store expression...'</p> <p><em><strong>For LINQ scenarios, queries against the Entity Framework involve mapping certain CLR methods to methods on the underlying data source through canonical functions. Any method calls in a LINQ to Entities query that are not explicitly mapped to a canonical function will result in a runtime NotSupportedException exception being thrown.</strong></em> </p> <p>Source: <a href="http://msdn.microsoft.com/en-us/library/bb738681.aspx" rel="nofollow">MSDN</a></p> <p>TimeSpan is one of those cases.</p> <p>There are at least two solutions, and these are...</p> <ol> <li><p>Use a valid LINQ to SQL (i.e., canonical methods only) statement to obtain the full set of results from the database; and then use LINQ to Objects to filter the results you get back. The LINQ to Objects filter should contain all of the non-canonical methods so that your filtering delivers the intended subset of data.</p></li> <li><p>Evacuate the non-canonical methods to a stored procedure (i.e., translate to straight SQL) and call the stored procedure.</p></li> </ol> <p>The third solution would be to try and wrap the non-canonical expressions into a private method in your domain service class. This is what I did before getting to the bottom of this elusive exception.</p> <p>A fourth, and more obscure approach, is given <a href="http://msdn.microsoft.com/en-us/library/dd456857.aspx" rel="nofollow">here</a> </p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/8840526/Why%20can't%20I%20project%20a%20%60TimeSpan%60%20across%20RIA%20using%20%60TimeSpan.FromMinutes%60?/8841512">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus