RL89
RL89

Reputation: 1916

LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method in MVC c#

When I am trying to Query Databae Context using Linq to Entities I am getting this Exception.
LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression.

Please help. Thanks in Advance

                if (Request.Form["Enroll"] != null)
                {
                    string[] selected = Request.Form["Enroll"].Split(',');

                    if (selected != null)
                    {
                        if (selected.Count() != 0)
                        {
                            int k = 0;
                            foreach (var item in selected)
                            {
                                var id = db.EnrollTrainee.Where(i => i.TraineeID ==          Convert.ToInt32(item[k].ToString())
                                         && i.TrainerID == Convert.ToInt32(Session["user"].ToString()));
                                if (id != null)
                                {
                                    foreach (var a in id)//Getting Exception Here
                                    {
                                        enroll.id = a.id;
                                        db.EnrollTrainee.Remove(enroll);
                                        db.SaveChanges();                                           
                                    }
                                }
                                k++;
                            }
                        }
                    }

Upvotes: 2

Views: 1054

Answers (1)

Gaz Winter
Gaz Winter

Reputation: 2989

Have you tried doing the conversion before you do the linq?

So like this:

  foreach (var item in selected)
  {
        var tempId = Convert.ToInt32(item[k].ToString());
        var tempId2 = Convert.ToInt32(Session["user"].ToString());
        var id = db.EnrollTrainee.Where(i => i.TraineeID == tempId         
                                     && i.TrainerID == tempId2);
                            if (id != null)
                            {
                                foreach (var a in id)//Getting Exception Here
                                {
                                    enroll.id = a.id;
                                    db.EnrollTrainee.Remove(enroll);
                                    db.SaveChanges();                                           
                                }
                            }
                            k++;
                        }

Upvotes: 2

Related Questions