LINQ to Entities无法识别MVC c#中的方法'Int32 Int32(System.String)'方法

时间:2012-09-05 09:44:51

标签: asp.net-mvc asp.net-mvc-3 c#-4.0 linq-to-entities

当我尝试使用Linq to Entities查询Databae Context时,我得到了这个例外            LINQ to Entities无法识别方法'Int32 Int32(System.String)'方法,并且此方法无法转换为商店表达式。

请帮忙。 在此先感谢

                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++;
                            }
                        }
                    }

1 个答案:

答案 0 :(得分:2)

您是否尝试过在执行linq之前进行转换?

所以这样:

  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++;
                        }