2

当我尝试使用 Linq to Entities 查询 Databae 上下文时,我得到了这个异常。
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++;
                            }
                        }
                    }
4

1 回答 1

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++;
                        }
于 2012-09-05T09:50:40.353 回答