I am trying to build a Linq to Entities query that joins a table with a foreign key column of string and the related table ID has an integer ID. I know, I know, whoever designed the database schema screwed up, but unfortunately for whatever reasons, I can’t change the database schema in this case.
Here is the error I get:
LINQ to Entities does not recognize the method ‘System.String ToString()’ method, and this method cannot be translated into a store expression.
Here is the linq (to Entities) statement that causes it:
var query = from p in db.QBPOes
join j in db.tblJobManagements on p.CustJob equals j.JobID
join s in db.tblSuppliers on p.VendorID equals s.SupplierID.ToString()
where p.ID == poID
select new
{
p.ID,
p.Date,
p.VendorID,
p.FirstName,
p.LastName,
s.SupplierWorkPhone,
s.SupplierFaxNumber,
p.CompanyName,
};
I am joining these tables because I want to get the supplier phone and fax number. Any help would be greatly appreciated.
In order to cast p.VendorID into an
integeryou can use this workaround. The generated SQL might not be pretty, but it should work, unless p.VendorID can not be cast into an integerIt might be better to cast s.SupplierID to string, so you would rearrange your query like so