Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework ToString method

Following code block throws error.

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

db.tbOnIgmHawbDetails
  .Where(s => !db.tbImpoExaminations.Any(x => x.Hawb.ToString() == s.Hawb) && s.AwbNo == p)
  .Select(s => s.Hawb).ToList();

Any suggestion? why this happen and what is the solution?

like image 460
Sagar Upadhyay Avatar asked Nov 27 '22 21:11

Sagar Upadhyay


2 Answers

.ToString() is supported properly going forward with EF 6.1: http://blogs.msdn.com/b/adonet/archive/2014/03/17/ef6-1-0-rtm-available.aspx

like image 134
Scott Stafford Avatar answered Dec 09 '22 06:12

Scott Stafford


You could try with SqlFunctions.StringConvert... Use the decimal conversion:

SqlFunctions.StringConvert((decimal)p.x.Hawb).TrimLeft() == ...

(the TrimLeft is necessary because the STR function of SQL will right align the number)

like image 27
xanatos Avatar answered Dec 09 '22 06:12

xanatos