5

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?

xanatos
  • 109,618
  • 12
  • 197
  • 280
Sagar Upadhyay
  • 335
  • 1
  • 4
  • 13
  • possible duplicate of [LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression](http://stackoverflow.com/questions/5899683/linq-to-entities-does-not-recognize-the-method-system-string-tostring-method) – Dan Drews Aug 11 '13 at 06:32
  • Why are you comparing the tostring representations of the variable why not just `x.hawb == s.hawb`? – undefined Aug 11 '13 at 06:33
  • x.hawb is type of long, and x.hawb is type of string, so.... – Sagar Upadhyay Aug 11 '13 at 06:39
  • I think you should override ToString function, for example, define new function named 'HawbToString' for Type of object of x. ok? – Maziar Aboualizadehbehbahani Aug 11 '13 at 06:41
  • You have tagged Linq-To-Sql but you get a Linq-To-Entities excption? – Tim Schmelter Aug 11 '13 at 06:48
  • @DanDrews There aren't good solutions in the suggested duplicates. They all suggest to make it a LINQ-To-Objects. Here it's not possible – xanatos Aug 11 '13 at 06:55
  • You dont have any navigation properties? This is wrong way to use EF, you are supposed to create proper FK relations and use navigation properties to query them. – Akash Kava Aug 11 '13 at 07:49
  • possible duplicate of [Why would Entity Framework not be able to use ToString() in a LINQ statement?](http://stackoverflow.com/questions/1920775/why-would-entity-framework-not-be-able-to-use-tostring-in-a-linq-statement) – Matt Mar 06 '15 at 09:28

5 Answers5

15

.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

Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
10

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)

xanatos
  • 109,618
  • 12
  • 197
  • 280
1

If s.Hawb is already string type (the error message suggests so), then remove the part .ToString() from your query.

The reason for it is that in LINQ2SQL, you can only use those language constructs that can be translated into SQL. For example, if you try to use RegEx in your C# expression, then SQL does not have a corresponding construct for RegEx, and thus LINQ cannot translate and execute your query.

dotNET
  • 33,414
  • 24
  • 162
  • 251
1

Easily add .AsEnumerable() before the .ToString() and those methods that L2E doesn't support:

var asen = db.tbOnIgmHawbDetails.AsEnumerable();

var result =  asen.Where(s => !asen.Any(x => x.Hawb.ToString() == s.Hawb) && s.AwbNo == p)
  .Select(s => s.Hawb).ToList();

That should works. However if not, try to perform your query by linq-to-objects syntax:

var result = from a in asen
             where ...
             select ...;
Amin Saqi
  • 18,549
  • 7
  • 50
  • 70
  • 2
    Won't `AsEnumerable()` cause the next call to run the query? That will pull down the entire table which defeats the point of using a database to begin with... – Steven Hunt Apr 13 '15 at 19:51
  • 1
    @StevenHunt of course it does! L2E only translate your L2E command to raw sql so it doesn't recognize `.ToString()` or any other function... You should pull desired records from db and perform such functions to the in-memory list... it's just one of several ways to achieve that goal... – Amin Saqi Apr 14 '15 at 04:56
0

do not use ToString

just use like

x.Hawb + "" == s.Hawb

mrTurkay
  • 642
  • 1
  • 7
  • 13