Just save the string to a temp variable and then use that in your expression:
var strItem = item.Key.ToString();
IQueryable<entity> pages = from p in context.pages
where p.Serial == strItem
select p;
The problem arises because ToString()
isn’t really executed, it is turned into a MethodGroup and then parsed and translated to SQL. Since there is no ToString()
equivalent, the expression fails.
Note:
Make sure you also check out Alex’s answer regarding the SqlFunctions
helper class that was added later. In many cases it can eliminate the need for the temporary variable.