Search code examples
c#linq-to-entities

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?


Solution

  • 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)