"entity framework 4 / linq: how to convert from datetime to string in a query?" Code Answer
1
tostring() is not supported in linq to entities - there is a list of function helpers as part of sqlfunctions but this doesn't support date to string conversion.
easiest would be to first project to an anonymous type within the query and then cast to an ienumerable by using asenumerable() - after that you can use tostring() because you are now using linq to objects for the remainder of the query expression (there's a lengthy article on this topic here).
var results = products.select( p => new { a.id, a.modified })
.asenumerable()
.select(p => new productvm()
{ id = p.id,
modified = p.modified.tostring()
});
tostring()
is not supported in linq to entities - there is a list of function helpers as part of sqlfunctions but this doesn't support date to string conversion.easiest would be to first project to an anonymous type within the query and then cast to an
ienumerable
by usingasenumerable()
- after that you can usetostring()
because you are now using linq to objects for the remainder of the query expression (there's a lengthy article on this topic here).