LINQ到实体无法识别方法“的Int32的Int32(System.String)”在MVC C#方

2019-09-22 04:10发布

当我试图查询使用LINQ到实体我得到这个异常Databae语境。
LINQ到实体无法识别方法“的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++;
                            }
                        }
                    }

Answer 1:

你是否尝试过做转换你做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++;
                        }


文章来源: LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method in MVC c#