Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to int in LINQ to Entities?

I have to convert a string value to int, but it seems LINQ to Entities does not support this.

For the following code, I am getting an error.

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.

like image 807
Crime Master Gogo Avatar asked Nov 04 '22 14:11

Crime Master Gogo


1 Answers

Do the conversion outside 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;
like image 106
Ankur Avatar answered Nov 14 '22 20:11

Ankur