Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework: LINQ to Entities only supports casting Entity Data Model primitive types
    text
    copied!<p>I wrote a method to allow for an Expression to be passed in for the orderby clause, but I ran into this problem.</p> <blockquote> <p>Unable to cast the type 'System.DateTime' to type 'System.IComparable'. LINQ to Entities only supports casting Entity Data Model primitive types.</p> </blockquote> <p>Basically the expression is this:</p> <pre><code>Expression&lt;Func&lt;K, IComparable&gt;&gt; orderBy </code></pre> <p>And is used like this:</p> <pre><code>SomeEntities.SomeTable .Where ( whereClause ) .Select ( selectClause ) .OrderBy(orderBy) </code></pre> <p>The idea is so that I can use a dictionary to hold string matches to expressions like:</p> <pre><code>_possibleSortForForumItem.Add("CreateDate", item =&gt; item.CreateDate); </code></pre> <p>Then I have a method that takes in the sort string and returns the expression if it matches a key in the dictionary, if not returns some default. (The idea being a way to control what it can be ordered by) Now this works for String properties, but so far not for datetime or integer as I get the error message above. </p> <p>Now far as I (loosely) understand the problem is that Entity Framework needs it to be a Primary/EDM type because it has to convert the C# DateTime into something the database can handle.</p> <p>Is there a way to convert the datetime to a primitive type so that this will still work?</p> <p><strong>Solution</strong></p> <p>The method for getting the order by method: (Take in a query and return it in "ordered form")</p> <pre><code>private static Func&lt;IQueryable&lt;ForumViewItem&gt;, IOrderedQueryable&lt;ForumViewItem&gt;&gt; GetMethodForSort(String sortBy) { if (_methodForSort == null) { _methodForSort = new Dictionary&lt;String, Func&lt;IQueryable&lt;ForumViewItem&gt;, IOrderedQueryable&lt;ForumViewItem&gt;&gt;&gt;(); _methodForSort.Add(SortForumViewItemCreatedOn, item =&gt; item.OrderBy(innerItem =&gt; innerItem.CreatedOn)); ... } Func&lt;IQueryable&lt;ForumViewItem&gt;, IOrderedQueryable&lt;ForumViewItem&gt;&gt; orderMethod; if(String.IsNullOrEmpty(sortBy) || !_methodForSort.ContainsKey(sortBy)) { orderMethod = _methodForSort["ForumName"]; } else { orderMethod = _methodForSort[sortBy]; } return orderMethod; } </code></pre> <p>The method signature for the generic query method:</p> <pre><code>IList&lt;K&gt; GetListForGrid&lt;T, K&gt;(this ObjectQuery&lt;T&gt; query, ... Func&lt;IQueryable&lt;K&gt;, IOrderedQueryable&lt;K&gt;&gt; orderBy, ...) </code></pre> <p>And the use of the passed in method:</p> <pre><code>initialQuery = query .Where ( somethingEqualsSomething ) .Select ( selectClause ); var orderedQuery = orderBy(initialQuery); returnValue = orderedQuery .Skip(numberToShow * realPage) .Take(numberToShow) .ToList(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload