0

这是我的代码

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 to Entities 不支持 LINQ 表达式节点类型“ArrayIndex”。

我该如何解决此问题或完成此任务的另一种方法是什么

谢谢

4

1 回答 1

0

您只需要将值复制到局部变量:

   var leaveIdValue = leaveIdArray[i];

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

顺便说一句,您可能应该leaveIdArray退出循环,因为它不依赖于i.

于 2013-08-05T09:17:46.600 回答