简体   繁体   中英

LINQ to Entities string to decimal a column

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.

You have to get the data to a List<Order> first and after that you can cast whatever you want.

var orders = 
    from s in
        ((from o in db.Orders
        where o.Order_Complete
        select o).ToList())
    select new { 
        s.Order_Id, 
        s.MySEL_Name, 
        s.MySEL_EMail, 
        s.MySEL_Bus_Name,
        Double.Parse(s.Total_Amt),
        s.Order_Complete_DateTime
    };

I think the EMS way would look much better in your code ;)

var orders =
    db.Orders.Where(s => s.Order_Complete).ToList().Select(s => new { /*...*/ }

After casting ToList() you have got the data object based and can modify it, if you don't you can use Double.Parse because EF is trying to find eg a stored procedure on the database and will throw an exception.

Have you tried model-defined functions?

<Function Name="ConvertToDecimal" ReturnType="Edm.Decimal">
     <Parameter Name="myStr" Type="Edm.String" />
     <DefiningExpression>
          CAST(myStr AS Edm.Decimal(12, 2))
     </DefiningExpression>
 </Function>

Check this answer: Convert string to decimal in group join linq query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM