2013-05-14 2 views
1

C# 및 OrganizationServiceContext.CreateQuery 메서드를 사용하여 CRM 2011에서 빈 열 "null 값"을 검색하는 데 문제가 있습니다. 내 코드는 아래와 같습니다. 문제는 SubType입니다. 그것이 null이면 "객체의 인스턴스로 설정되지 않은 객체"라는 오류가 발생합니다. 아무도 도와 줄 수 있니?OrganizationServiceContext에서 "null values"검색

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 인 것으로 추측하고 있습니다. 값이 null 인 대신 null이되도록 확인해야합니다. 사용해보기 :

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

빠른 응답을 보내 주셔서 감사합니다. 이제 내 새로운 문제는 r.new_type이 null이 아닌 레코드를 가져 오는 것입니다. OptionSetValue r.new_type이 null 인 레코드를 어떻게 얻을 수 있습니까? 다시 한번 협조 해주십시오. –

+0

신속한 답변을 위해 Daryl에게 감사드립니다. 이제 내 새로운 문제는 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 및 null이 아닌 레코드 또는 new_type이 null 인 레코드 만 필요합니까? – Daryl