5

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.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Reza.Hoque
  • 2,690
  • 10
  • 49
  • 78

2 Answers2

4

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;
CarenRose
  • 1,266
  • 1
  • 12
  • 24
Ankur
  • 33,367
  • 2
  • 46
  • 72
  • yes, that would definitely work :). But I wonder there must be a way to do this inside the query.. – Reza.Hoque Sep 13 '11 at 09:35
  • 1
    It depends on the LINQ provider what will work inside the linq and what will not (i.e whether the linq provider is able to convert the expression to under lying language or not) EF provider it doesn't support this convert expression. – Ankur Sep 13 '11 at 09:39
  • @kandroid I think its not good to do it in the query, though you could possibly do `p.ID.ToString() == id` – Manatherin Sep 13 '11 at 09:42
  • Oh and the error is because linq is trying to convert your statement to SQL but it doesn't recognise that convert is equivalent to SQL cast, not sure if it would recognise parse as equivalent or not – Manatherin Sep 13 '11 at 09:44
  • Thank you guys. p.ID.ToString()==id, don't work and even int.parse(id) don't. But,I found my answer. cheers. – Reza.Hoque Sep 13 '11 at 09:56
  • this is not the correct answer. The question is about the right expression syntax. Not a workaround – Manoochehr Dadashi Apr 21 '17 at 12:51
  • @abzarak I don't see any sentence in the question that asks about "right expression syntax". If you do have the correct answer please let us know. – Ankur Apr 21 '17 at 15:06
-2

No they wouldn't. Think of it this way: both ToString() and Parse() are methods on the objects. Since LINQ to Entities tries to convert your LINQ expression to SQL, those are not available.

If one needs to do this in the query, it might be possible with Cast, which should be available in LINQ to Entities. In the case of ToString, you could use SqlFunctions.StringConvert().

CarenRose
  • 1,266
  • 1
  • 12
  • 24
dings
  • 13
  • 1
  • 3
    -1. Cast converts the elements of a supplied collection to the specified type. If you supply a string to Cast it will try to convert that string to an IEnumerable of requested type. – Dejan Janjušević Feb 11 '13 at 16:36