2013-08-05 41 views
0

這是我的代碼如何在LINQ使用變量值實體

if (Count < LeaveTypeCount) 
{ 
    for (int i = 0; i < LeaveTypeCount; i++) 
    { 
     var LeaveId = from l in CompObj.LeaveTypes 
         select l.LeaveID; 
     var leaveIdArray = LeaveId.ToArray(); 

     var LeaveDefault = (from c in CompObj.LeaveTypes 
          where (c.LeaveID ==leaveIdArray[i]) 
          select new { c.DefaultLeave }).FirstOrDefault(); 

     Int32 DefaultCount = Convert.ToInt32(LeaveDefault.DefaultLeave); 


     AssignedLeave AddObj = new AssignedLeave(); 
     AddObj.EmpID = EmpID; 
     AddObj.AssignedYear = LeaveYear; 
     AddObj.LeaveID =leaveIdArray[i]; 
     AddObj.TotalLeave = DefaultCount; 
     CompObj.AssignedLeaves.AddObject(AddObj); 
     CompObj.SaveChanges(); 

    } 
} 

但其示出了錯誤的LINQ表達式節點類型「ArrayIndex」在LINQ不支持對實體。

我該如何解決這個問題,或者有什麼其他的方式做這個任務

感謝

回答

0

你只需要值複製到本地變量:BTW

var leaveIdValue = leaveIdArray[i]; 

    var LeaveDefault = (from c in CompObj.LeaveTypes 
         where (c.LeaveID ==leaveIdValue) 
         select new { c.DefaultLeave }).FirstOrDefault(); 

,您應該將leaveIdArray移出循環,因爲它不取決於i的值。

+0

非常感謝您的幫助 – user2516261