LINQ to Entities string to decimal a column

前端 未结 2 882
别跟我提以往
别跟我提以往 2021-01-19 04:19

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 op

相关标签:
2条回答
  • 2021-01-19 04:53

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

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

    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.

    0 讨论(0)
  • 2021-01-19 04:56

    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

    0 讨论(0)
提交回复
热议问题