Int.Parse in Linq Expression

后端 未结 7 1833
花落未央
花落未央 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:47

    There is a badly built database, which I cannot edit, but have to use it.

    I use this in Linq-To-Sql and it works.

    First cast the string to object, then cast it to int.

     from s in db.Students
     select new
     {
         s.Name,
         s.Surname,
         Birthday = new DateTime((int)(object)(s.dateStr.Substring(0, 4)),
                                 (int)(object)(s.dateStr.Substring(4, 2)),
                                 (int)(object)(s.dateStr.Substring(6, 2))),
     }
    

提交回复
热议问题