0

I am running into one headache issue regarding data type conversion in linq select statement, please see it as below, I would like to convert the string type of one below field with hightlight to double type, so i leverage Convert.ToDouble method to do it, however, it failed . any help or suggestion is appreciated.

Thanks in advance.

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
                          });
beyond8848
  • 103
  • 7
  • *"however, it failed"* Failed with what? Please include the **full exception message**. – Ivan Stoev Feb 24 '16 at 08:48
  • LINQ to Entities does not recognize the method 'Double Parse(System.String)' method, and this method cannot be translated into a store expression. – beyond8848 Feb 24 '16 at 08:57
  • Possible duplicate of [LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method,](http://stackoverflow.com/questions/30136302/linq-to-entities-does-not-recognize-the-method-int32-parsesystem-string-meth) – Aron Feb 24 '16 at 09:53

2 Answers2

1

Try replacing that line with

StandardWeight = (Double)d.StandardWeight,

The reason that the previous code does not work is that EntityFramework* does not support .net Framework methods (by in large).

It does however support Expression.Cast operations, which C# would not.

*My personal branch of EntityFramework does support this operation, but I can't check it into the mainline.

Aron
  • 15,464
  • 3
  • 31
  • 64
  • 1
    The problem is that the C# compiler does not support what you wrote. You can full it by using `(Double)(object)d.StandardWeight` but then you'll get similar runtime exception from EF. – Ivan Stoev Feb 24 '16 at 09:41
  • 1
    @IvanStoev That is unfortunate. The easiest way to fix this then would be to fix EF http://stackoverflow.com/questions/30136302/linq-to-entities-does-not-recognize-the-method-int32-parsesystem-string-meth/30136689#30136689 – Aron Feb 24 '16 at 09:54
-1

You can use let. Please try this:

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

EDIT:Aron's Solution is right(+1) I used like this and now remember it :)

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

                           }; 
  • Thanks Alper for quick help, i followed your approach to try it, but we met the same exception. "LINQ to Entities does not recognize the method 'Double ToDouble(System.String)' method, and this method cannot be translated into a store expression." – beyond8848 Feb 24 '16 at 09:10
  • check my edit pls just add tolist() i think this solve this problem @beyond8848 – Alper Tunga Arslan Feb 24 '16 at 09:20
  • That does not change your Linq Expression at all. – Aron Feb 24 '16 at 09:20
  • Thanks to Alper, although i appended tolist() method , it still did not work.:( – beyond8848 Feb 24 '16 at 09:39
  • @AlperTungaArslan, just confirm if you are using personal branch of EF ,right? as far as know ,most of guys are little possible to fork EF code and add some code to reach data conversion . – beyond8848 Feb 24 '16 at 13:25
  • Yeah i m using EF 6.0 and i could use this conversion like this @beyond8848 – Alper Tunga Arslan Feb 24 '16 at 13:35
  • I got answer from Microsoft community , please take a look https://social.msdn.microsoft.com/Forums/en-US/7ab97cd4-9f15-4b62-9b57-ff03e1bcf15b/unable-to-cast-string-type-to-number-type-in-linq-expression?forum=adodotnetentityframework – beyond8848 Feb 25 '16 at 03:10