LINQ to Entities字符串将十进制列 [英] LINQ to Entities string to decimal a column

查看:58
本文介绍了LINQ to Entities字符串将十进制列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了这个问题,直到死了.每个人和他们的兄弟都想知道如何将int或小数转换为字符串,但是我找不到使用EF进行相反操作的任何示例.

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.

我的数据源在数据库中具有varchar类型的order total_amt列.原因是因为源数据已加密.解密器就位.我可以将其重写为解密到临时表,然后将这些结果插入到正确键入的表中,但这将需要更多的工作,无论是现在还是在扩展应用程序时进行数据库更新.

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.

我很希望能够进行列转换,但是我不知道如何使用Linq和EF来做到这一点.

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);
}

注意:结果必须是iQueryable.我真的不想ToList这个,因为那样会从数据库中提取所有数据.这已绑定到一个Telerik KendoUI网格,该网格正在传递分页,排序和其他参数(请注意,它是使用传递的请求将其强制转换为KendoUI的ToDataSourceResult的,该请求包含上述分页,排序等数据.

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.

推荐答案

您必须先将数据保存到List<Order>,然后才能转换任何内容.

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
    };

我认为EMS方式在您的代码中看起来会更好;)

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 { /*...*/ }

强制转换ToList()之后,您将获得基于数据对象的数据对象并可以对其进行修改,如果不这样做,则可以使用Double.Parse,因为EF试图查找例如数据库中的存储过程,将引发异常.

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 e.g. a stored procedure on the database and will throw an exception.

这篇关于LINQ to Entities字符串将十进制列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆