2

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++;
                            }
                        }
                    }
tereško
  • 58,060
  • 25
  • 98
  • 150
RL89
  • 1,866
  • 5
  • 22
  • 39

1 Answers1

2

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++;
                        }
Gaz Winter
  • 2,924
  • 2
  • 25
  • 47