2013-05-14 33 views
1

我在使用C#和OrganizationServiceContext.CreateQuery方法檢索CRM 2011中的空列「空值」時出現問題。我的代碼如下。問題出在SubType。當它爲空時,我得到一個錯誤:「對象未設置爲對象的實例」。任何人都可以請協助?從OrganizationServiceContext檢索「空值」

public List<Opportunities> GetOpportunitiesList() 
    { 
     using (_orgService = new OrganizationService(connection)) 
     { 
      OrganizationServiceContext context = new OrganizationServiceContext(_orgService); 
      // Create Linq Query. 
      DateTime getdate = DateTime.Now.AddDays(-3); 
      var query = (from r in context.CreateQuery<Opportunity>() 
         join c in context.CreateQuery<Contact>() on r.ContactId.Id equals c.ContactId 
          into rc 
         from z in rc.DefaultIfEmpty() 
         where r.CreatedOn > getdate && r.new_type.Value != null 
         orderby r.ContactId 
         select new Opportunities 
         { 
          opporunity = r.new_OpportunityNumber, 
          //region =GetAccountRegionsbyId(a.new_region.Value), 
          accountid = r.CustomerId.Id, 
          topic = r.Name, 
          status = r.StateCode.Value.ToString(), 
          pcustomer = r.CustomerId.Name, 
          estReve = (decimal)r.EstimatedValue.Value, 
          owner = r.OwnerId.Name, 
          salesstagecode = r.SalesStageCode.Value, 
          pipelinePhase = r.StepName, 
          opptype = getAllOptionSetValues(r.LogicalName, "new_sf_recordtype", r.new_sf_recordtype.Value), 
          subtype = getAllOptionSetValues(r.LogicalName, "new_type", r.new_type.Value == null ? default(int) : r.new_type.Value), 
          estclosedate = r.EstimatedCloseDate.Value, 
          actualRev = (Microsoft.Xrm.Sdk.Money)r.ActualValue, 
          probability = r.CloseProbability.Value, 
          weighedRev = (decimal)r.new_WeightedValue.Value, 
          Email = z.EMailAddress1, 
          CreatedOn = r.CreatedOn.Value, 
          modifiedBy = r.ModifiedBy.Name, 

          modifiedOn = r.ModifiedOn.Value 
         }).ToList(); 
      foreach (Opportunities o in query) 
      { 
       o.region = (from d in context.CreateQuery<Account>() where d.AccountId == o.accountid select getAllOptionSetValues(d.LogicalName, "new_region", d.new_region.Value)).FirstOrDefault(); 

      } 
      return query; 
     } 
    } 

回答

0

我在猜測r.new_type是一個OptionSetValue。你需要檢查它爲空而不是空值。試試這個:

public List<Opportunities> GetOpportunitiesList() 
{ 
    using (_orgService = new OrganizationService(connection)) 
    { 
     OrganizationServiceContext context = new OrganizationServiceContext(_orgService); 
     // Create Linq Query. 
     DateTime getdate = DateTime.Now.AddDays(-3); 
     var query = (from r in context.CreateQuery<Opportunity>() 
        join c in context.CreateQuery<Contact>() on r.ContactId.Id equals c.ContactId 
         into rc 
        from z in rc.DefaultIfEmpty() 
        where r.CreatedOn > getdate && r.new_type != null 
        orderby r.ContactId 
        select new Opportunities 
        { 
         opporunity = r.new_OpportunityNumber, 
         //region =GetAccountRegionsbyId(a.new_region.Value), 
         accountid = r.CustomerId.Id, 
         topic = r.Name, 
         status = r.StateCode.Value.ToString(), 
         pcustomer = r.CustomerId.Name, 
         estReve = (decimal)r.EstimatedValue.Value, 
         owner = r.OwnerId.Name, 
         salesstagecode = r.SalesStageCode.Value, 
         pipelinePhase = r.StepName, 
         opptype = getAllOptionSetValues(r.LogicalName, "new_sf_recordtype", r.new_sf_recordtype.Value), 
         subtype = getAllOptionSetValues(r.LogicalName, "new_type", r.new_type == null ? default(int) : r.new_type.Value), 
         estclosedate = r.EstimatedCloseDate.Value, 
         actualRev = (Microsoft.Xrm.Sdk.Money)r.ActualValue, 
         probability = r.CloseProbability.Value, 
         weighedRev = (decimal)r.new_WeightedValue.Value, 
         Email = z.EMailAddress1, 
         CreatedOn = r.CreatedOn.Value, 
         modifiedBy = r.ModifiedBy.Name, 

         modifiedOn = r.ModifiedOn.Value 
        }).ToList(); 
     foreach (Opportunities o in query) 
     { 
      o.region = (from d in context.CreateQuery<Account>() where d.AccountId == o.accountid select getAllOptionSetValues(d.LogicalName, "new_region", d.new_region.Value)).FirstOrDefault(); 
     } 
     return query; 
    } 
} 
+0

謝謝Daryl爲您的快速反應。現在我的新問題是,我正在獲取r.new_type不爲null的記錄。如何獲取OptionSetValue r.new_type爲null的記錄?請再次協助。 –

+0

謝謝戴瑞爾的快速回復。現在我的新問題是,我正在獲取r.new_type不爲null的記錄。如何獲取OptionSetValue r.new_type爲null的記錄?我相信以下行:當r.new_type.Value爲null時,subtype = getAllOptionSetValues(r.LogicalName,「new_type」,r.new_type.Value!= null?r.new_type.Value:default(int))失敗,並且指定一個默認的int如果現在的值似乎不能解決問題。我卡住了,並希望得到額外的幫助。 –

+0

@IsaiahGuantai您是否想要null和non-null記錄,或只有new_type爲null的記錄? – Daryl