Int.Parse in Linq Expression

后端 未结 7 1851
花落未央
花落未央 2020-12-01 21:18

I have the following LINQ expression. I want calculate the sum of numeric values in an nvarchar field. I\'m using following code to do this, but I get an error

7条回答
  •  暖寄归人
    2020-12-01 21:50

    EF 5:

    Instead of int.Pasrse use Convert.ToInt32. Entity Framework will generate proper CAST functions in SQL.

    EF 6:

    Short answer:

    youEntity.Where(c=>SqlFunctions.StringConvert((decimal?)c.INTFIELD).Trim() == STRINGVALUE)
    

    Long answer:

    in EF 6 you have to convert numeric value to string with SqlFunctions.StringConvert. but it has a problem. It will add unnecessary spaces to the result. so the comparison will fail. That's why I have put Trim() there. I have tested it with EF 6.1.1.

提交回复
热议问题