Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
while running it gives exception like below:


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


What I have tried:

C#
demo db = (from c in ob.demoes
                       where c.id ==Convert.ToInt32(txtboxId)
                       select c).FirstOrDefault();
            db.name = txtboxName.Text;
            db.salary = int.Parse(txtboxSalary.Text);
            ob.SaveChanges();
Posted
Updated 21-Dec-16 1:37am

1 solution

Try doing the conversion first:
C#
int id = Convert.ToInt32(txtboxId);
demo db = (from c in ob.demoes
           where c.id == id
           select c).FirstOrDefault();
db.name = txtboxName.Text;
db.salary = int.Parse(txtboxSalary.Text);
ob.SaveChanges();

But it should be unnecessary. Certainly, it works fine in normal Linq:
C#
int id = 66;
string sid = "66";
List<MyClass> list = new List<MyClass>();
MyClass mc = new MyClass();
mc.id = 55;
list.Add(mc);
mc = new MyClass();
mc.id = 66;
list.Add(mc);
var x = (from c in list
        where c.id == id
        select c).FirstOrDefault();
var y = (from c in list
         where c.id == Convert.ToInt32(sid)
         select c).FirstOrDefault();

Compiles and runs fine, and x and y both contain the same instance.
 
Share this answer
 
Comments
Member 12821006 21-Dec-16 7:48am    
Thank @OriginalGriff it is working.
Richard Deeming 21-Dec-16 10:42am    
"Normal LINQ" - aka LINQ to Objects - doesn't have to translate the query into another language. LINQ to Entities does, and as a result, it only supports a limited number of methods, and throws an exception for any that it doesn't recognise. :)
OriginalGriff 21-Dec-16 10:49am    
I thought it had to be something like that - that's why I suggested converting it first. As well as "it's probably more efficient that way" :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900