简体   繁体   English

LINQ to Entities字符串到十进制列

[英]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. 每个人和他们的兄弟想知道如何将int或decimal转换为字符串,但我找不到任何与EF相反的例子。

My data source has an order total_amt column in the database that is of type varchar. 我的数据源在数据库中有一个order_amt列,其类型为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. 我希望能够投射列,但我无法弄清楚如何使用Linq和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. 注意:结果需要是iQueryable。 I really don't want to ToList this as that would pull all the data from the DB. 我真的不想ToList这,因为它会从数据库中提取所有数据。 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. 这被绑定到Telerik KendoUI Grid,它正在传递分页,排序和其他参数(注意它是使用传入的请求转换到KendoUI的ToDataSourceResult,该请求保存上面提到的分页,排序等数据。

You have to get the data to a List<Order> first and after that you can cast whatever you want. 您必须首先将数据提取到List<Order> ,之后您可以投射任何您想要的内容。

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 ;) 我认为EMS方式在你的代码中看起来要好得多;)

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. 在转换ToList()您已经获得了数据对象并且可以对其进行修改,如果不这样做,您可以使用Double.Parse因为EF试图在数据库中查找例如存储过程并抛出异常。

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 检查此答案: 在组连接linq查询中将字符串转换为十进制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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