无法在Linq表达式中将字符串类型转换为数字类型

时间:2016-02-24 08:40:50

标签: string linq casting double

我遇到一个关于linq select语句中数据类型转换的头疼问题,请看下面的内容,我想将hightlight下面一个字段的字符串类型转换为double类型,所以我利用Convert.ToDouble方法然而,它失败了。任何帮助或建议表示赞赏。

提前致谢。



var orders = (from q in dao.CurrentDBContext.Order
                          from d in dao.CurrentDBContext.OrderGoodsDetail 
                          where q.billNum == d.billNum
                          select new
                          {
                              q.billNum,
                              q.orderSource,
                              q.sourceOddNum,
                              q.orderType,
                              q.createdTime,
                              q.physicalNum,
                              q.ShopName,
                              q.pay_time,
                              d.SpecificationCode,
                              d.SpecificationName,
                              d.Color,
                              d.Size,
                              d.CommodityCode,
                              d.SKU,
                              d.StandardSellingPrice,
                              d.pay_ment,
                              **StandardWeight= Convert.ToDouble(d.StandardWeight),** //the date type of d.StandardWeight is string , i would like to convert it to double type.
                              d.PurchaseNumber
                          });




2 个答案:

答案 0 :(得分:1)

尝试用

替换该行
StandardWeight = (Double)d.StandardWeight,

以前的代码不起作用的原因是EntityFramework *不支持.net Framework方法(大体上)。

但它确实支持Expression.Cast操作,而C#则不支持。

*我的EntityFramework个人分支确实支持此操作,但我无法将其检入主线。

答案 1 :(得分:-1)

您可以使用let。请试试这个:

var orders = (from q in dao.CurrentDBContext.Order
                          from d in dao.CurrentDBContext.OrderGoodsDetail.ToList() // adding tolist()
                          let d.StandardWeight =Convert.ToDouble(d.StandardWeight);
                          where q.billNum == d.billNum
                          select new
                          {
                              q.billNum,
                              q.orderSource,
                              q.sourceOddNum,
                              q.orderType,
                              q.createdTime,
                              q.physicalNum,
                              q.ShopName,
                              q.pay_time,
                              d.SpecificationCode,
                              d.SpecificationName,
                              d.Color,
                              d.Size,
                              d.CommodityCode,
                              d.SKU,
                              d.StandardSellingPrice,
                              d.pay_ment,
                              (Double)d.StandardWeight,
                              d.PurchaseNumber
                          });

编辑:Aron的解决方案是对的(+1)我这样使用,现在记住它:)

                           select new KaizensMinVM
                           {
                               ID = Kaizens.ID,
                               NAME = Kaizens.NAME,
                               KAIZEN_METHOD_ID =(decimal)Kaizens.KAIZEN_METHOD_ID//just use cast here

                           };