I have researched this question to death. Everyone and their brother wants to know how to convert an int or decimal to string, but I can't find any example of doing the opposite with EF.
My data source has an order total_amt column in the database that is of type varchar. The reason is because the source data was encrypted. The decryptor does so in place. I could rewrite that to decrypt to a temp table and then insert those results to a properly typed table but that would require allot more work, both now and during DB updates as we expand the app.
I'd love to be able to cast the columns but I can't figure out how to do that with Linq and EF.
public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request) {
var db = new Ecommerce_DecryptedEntities();
var orders = from s in db.Orders
where s.Order_Complete == true
select new {
s.Order_Id,
s.MySEL_Name,
s.MySEL_EMail,
s.MySEL_Bus_Name,
s.Total_Amt,
s.Order_Complete_DateTime
};
DataSourceResult result = orders.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}
Note: the result needs to be iQueryable. I really don't want to ToList this as that would pull all the data from the DB. This is being bound to a Telerik KendoUI Grid which is passing in paging, sorting and other params (notice it's being cast to KendoUI's ToDataSourceResult using the passed in request which hold the above mentioned paging, sorting, etc. data.