In LINQ to Entities, can I convert a string to an integer?

entity-framework linq-to-entities

Question

I must transform astring worth toint LINQ to Entities does not seem to support this however.

I'm receiving an error with the following code.

var query = (from p in dc.CustomerBranch
             where p.ID == Convert.ToInt32(id) // here is the error.
             select new Location()
             {
                 Name      = p.BranchName,
                 Address   = p.Address,
                 Postcode  = p.Postcode,
                 City      = p.City,
                 Telephone = p.Telephone
             }).First();
return query;

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

1
4
11/18/2019 8:36:37 PM

Accepted Answer

Convert the data outside of LINQ:

var idInt = Convert.ToInt32(id);
var query = (from p in dc.CustomerBranch
             where p.ID == idInt 
             select new Location()
             {
                 Name      = p.BranchName,
                 Address   = p.Address,
                 Postcode  = p.Postcode,
                 City      = p.City,
                 Telephone = p.Telephone
             }).First();
return query;
4
11/18/2019 11:44:32 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow