1

我第一次使用 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   

在进行 Google 搜索时,我似乎无法找到有关此特定错误的任何信息,并且不确定我做错了什么。是因为原始查询包含连接吗?

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

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

4

1 回答 1

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;
于 2012-10-25T13:56:56.250 回答