2012-09-05 87 views
2

當我試圖使用Linq to Entities查詢Databae上下文時,我得到此異常。
LINQ to Entities不能識別方法'Int32 Int32(System.String)'方法,並且此方法不能轉換爲存儲表達式。LINQ to Entities無法識別MVC中的方法'Int32 Int32(System.String)'方法c#

請幫忙。 在前提前感謝

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

回答

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

感謝man..Life保護... – RL89

+0

沒問題,我前幾天有同樣的問題;)高興能幫上忙。 –

相關問題