none
Unable to cast string type to number type in Linq expression RRS feed

  • Question

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

    • Edited by Nate Wu Wednesday, February 24, 2016 8:49 AM
    Wednesday, February 24, 2016 8:47 AM

Answers

  • sorry, i can not remove it , i need to covert to double type .

    Well it's never going to work for you as EF is clearly telling you that it can't do to, and it will not generate the T-SQL statement for the query.

    Linq-2-Object is possible where you Linq query the results of the first query without the Convert, and you do it object to object, old object property StandardWeight on a Convert to Double to new object property.

    But the first query with Linq where it is generating the T-SQL to query the database and do a Covnert to double in the middle of that query is a no-go for you, because EF and Linq can't do it. Linq and EF can't do everything, and you have hit one of those it can't do everything.

    Thursday, February 25, 2016 1:31 AM

All replies

  • the failure information is 

    LINQ to Entities does not recognize the method 'Double Parse(System.String)' method, and this method cannot be translated into a store expression. 

    Wednesday, February 24, 2016 2:24 PM
  • the failure information is 

    LINQ to Entities does not recognize the method 'Double Parse(System.String)' method, and this method cannot be translated into a store expression. 

    What it is telling you is that EF cannot make T-SQL that includes the **StandardWeight= Convert.ToDouble(d.StandardWeight),** that would be used to populate the anonymous object in a Linq projection from the generated T-SQL.

    You have to remove it and come up with something else.

    Wednesday, February 24, 2016 4:46 PM
  • sorry, i can not remove it , i need to covert to double type .
    Thursday, February 25, 2016 1:17 AM
  • sorry, i can not remove it , i need to covert to double type .

    Well it's never going to work for you as EF is clearly telling you that it can't do to, and it will not generate the T-SQL statement for the query.

    Linq-2-Object is possible where you Linq query the results of the first query without the Convert, and you do it object to object, old object property StandardWeight on a Convert to Double to new object property.

    But the first query with Linq where it is generating the T-SQL to query the database and do a Covnert to double in the middle of that query is a no-go for you, because EF and Linq can't do it. Linq and EF can't do everything, and you have hit one of those it can't do everything.

    Thursday, February 25, 2016 1:31 AM
  • Great, Thanks DA924 . i tried , Linq-2-Object is feasible.
    Thursday, February 25, 2016 3:08 AM
  • Hi Nate,

    >>LINQ to Entities does not recognize the method 'Double Parse(System.String)' method, and this method cannot be translated into a store expression

    There are there ways to solve the issue.

    First, use (double) instead of convert.todouble like this:

    var query = (from q in db.Suppliers
                                 join d in db.Products
                                 on q.Id equals d.SupplierId
                                 select new
                                 {
                                     q.Name,
                                     Price = (double)d.Price
                                 }).ToList();
    

    Second, use AsEnumerable(), like below.

    var query = (from q in db.Suppliers.AsEnumerable()
                                 join d in db.Products.AsEnumerable()
                                 on q.Id equals d.SupplierId
                                 select new
                                 {
                                     q.Name,
                                     Price = Convert.ToDouble(d.Price)
                                 }).ToList();
    

    The third, use the “DefiningExpression”. You could refer to the following link for detail.

    https://social.msdn.microsoft.com/Forums/en-US/85dbf9b4-94df-4c67-995c-6359eebbe9e6/cannot-be-translated-into-a-linq-to-entities-store-expression?forum=adodotnetentityframework

    Best regards,

    Cole Wu


    Thursday, February 25, 2016 3:15 AM
    Moderator
  • var query = (from q in db.Suppliers
                                 join d in db.Products
                                 on q.Id equals d.SupplierId
                                 select new
                                 {
                                     q.Name,
                                     Price = (double)d.Price
                                 }).ToList();

    Well that's not possible, given that the property Price is a string field. You will get compile error "Cannot convert type 'string' to 'double'".

    And in the second query, whole tables would be read from the database, which is kinda less efficient especially when I need just a few columns.

    Sunday, July 8, 2018 10:41 AM