在foreach循环中更新对象

时间:2012-10-25 13:34:40

标签: linq entity-framework-4 foreach

我第一次使用EF4 / LINQ并遇到了问题。我正在使用foreach循环循环LINQ查询的结果,如下所示:

   private static void OnTimedEvent(object source, ElapsedEventArgs e) 
   {
      CallOutcomeSubmission los = new CallOutcomeSubmission();      
      client = connectToService();

      try
      {
         using (var context = new CallOutcomeContext())
         {
            // List of available actions
            private static string ACTION_CALL_ATTEMPT = "Call Attempt";
            DateTime oneDayAgo = DateTime.Now.AddHours(-24);
            var query = from co in context.T_MMCallOutcome
                        join ca in context.T_Call on co.CallID equals ca.CallID
                        join lv in context.T_LeadVendorEmailHeader on co.LeadVendorEmailID equals lv.LeadVendorEmailID
                        where co.EnteredOn > oneDayAgo && co.MMLeadActionID == null
                        select new
                        {
                            co.CallOutcomeID,
                            co.CallID,
                            co.LeadVendorEmailID,
                            MMLeadID = lv.email_text,
                            ca.OutcomeID,
                            lv.FranchiseNumber,
                            co.MMLeadActionID,
                            co.LeadAction
                        };

            // if any results found for query
            if (query.Any())
            {
               foreach (var call in query.ToList())
               {
                  // if the franchise exists 
                  if (client.FranchiseExists(int.Parse(call.FranchiseNumber)))
                  {
                     switch (call.OutcomeID)
                     {
                        case 39:    // Not Answered
                           call.LeadAction = ACTION_CALL_ATTEMPT;  
                           break;
                        case 43:    // Remove from Call List
                           break;
                        default:    // If the OutcomeID is not identified in the case statement
                           break;
                     }  // switch

                  }
                  else
                  {
                     los.eventLog.WriteEntry("CallOutcomeSubmission.OnTimedEvent: No franchise found with franchise ID " + call.FranchiseNumber);
                  }

                  // Save any changes currently on context
                  context.SaveChanges();

               }  // foreach

            }

            // if no results found from query write system log stating such
            else 
            {
               los.eventLog.WriteEntry("CallOutcomeSubmission.OnTimedEvent: No new entries found");
            }
         }  // using

         client.Close();
      }
      catch (System.TimeoutException exception)
      {
         los.eventLog.WriteEntry("CallOutcomeSubmission.OnTimedEvent:" + exception.ToString());
                    client.Abort();
      }
      catch (System.ServiceModel.CommunicationException exception)
      {
         los.eventLog.WriteEntry("CallOutcomeSubmission.OnTimedEvent:" + exception.ToString());
                    client.Abort();
      }
    }

当我尝试完成作业时:

call.LeadAction = ACTION_CALL_ATTEMPT; 

我收到

的构建错误
Property or indexer 'AnonymousType#2.LeadAction' cannot be assigned to -- it is read only   

我似乎无法找到关于此搜索的特定错误的任何内容,而且我不确定我做错了什么。是因为原始查询包含连接吗?

如何在foreach循环中分配call.LeadAction?

我还想知道是否存在设计问题,我编写查询或执行任何操作,因为这是我第一次涉足EF / LINQ。

1 个答案:

答案 0 :(得分:0)

您正在创建一个新的匿名类型 - 使用Linq连接,然后尝试设置该值。您真正想要做的是更新呼叫的LeadAction是否正确?

EF如何知道将新查询转换回实体以便它可以返回数据库?它必须经历很多箍,而且它不具备这种能力。

你可以做的是从你的数据库中检索Call并设置LeadAction那样 - 我正在使用Find,假设CallID是你的PK:

                  case 39:    // Not Answered
                       var thisCall = context.T_Call.Find(call.CallID)
                       thisCall.LeadAction = ACTION_CALL_ATTEMPT;                              
                       break;