LINQ to Entities不支持ArrayIndex

时间:2014-05-07 07:41:12

标签: linq linq-to-entities

//从此数据库响应中更新信息

public bool UpdatePassengerInformation(GetResponse response)
{
 try
 {
      using (MyEntities context = new MyEntities())
      {

           var masterID = (from Id in context.BookMaster where Id.BookingId ==     response.BookingId select Id.MasterId).First();

           if (masterID != null)
           {
                for (int i = 0; i < response.Book.Length; i++)
                {
                     var passenger_info = from passenger in context.BookPassenger
                                          where passenger.BookID == masterID &&
                                          assenger.FirstName == response.Book[i].FirstName && passenger.LastName == response.Book[i].LastName select passenger;

//在下面的循环中我得到了异常“在LINQ to Entities中不支持LINQ表达式节点类型'Array Index'。”

                    foreach (BookPassenger book_passenger in passenger_info)
                    {
                        book_passenger.TicketId = response.Book[i].TicketId;
                        book_passenger.TicketNumber = response.Book[i].TicketNumber;
                    }
               }
               context.SaveChanges();
           }         
     }
     return true;
}
  catch (Exception ex)
  {
     throw ex;
  }
}

1 个答案:

答案 0 :(得分:0)

所以不要在查询中使用它,只需分配一个新变量并使用它。

for (int i = 0; i < response.Book.Length; i++)
{
      var responseBook = response.Book[i];
      var passenger_info = from passenger in context.BookPassenger
                           where passenger.BookID == masterID &&
                           assenger.FirstName == responseBook.FirstName && passenger.LastName == responseBook.LastName select passenger;
      foreach (BookPassenger book_passenger in passenger_info)
      {
           book_passenger.TicketId = responseBook.TicketId;
           book_passenger.TicketNumber = responseBook.TicketNumber;
      }
}