C# 从OrganizationServiceContext检索“空值”

C# 从OrganizationServiceContext检索“空值”,c#,dynamics-crm-2011,C#,Dynamics Crm 2011,我在使用C和OrganizationServiceContext.CreateQuery方法检索CRM 2011中的空列null值时遇到问题。我的代码如下。问题在于子类型。当它为空时,我得到一个错误:Object未设置为对象的实例。有人能帮忙吗 public List<Opportunities> GetOpportunitiesList() { using (_orgService = new OrganizationService(connecti

我在使用C和OrganizationServiceContext.CreateQuery方法检索CRM 2011中的空列null值时遇到问题。我的代码如下。问题在于子类型。当它为空时,我得到一个错误:Object未设置为对象的实例。有人能帮忙吗

    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;
        }
    }

我猜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;
    }
}

谢谢Daryl的快速回复。现在我的新问题是,我得到的记录中r.new_type不为null。如何获取OptionSetValue r.new_type为空的记录?请再次协助。谢谢Daryl的快速回复。现在我的新问题是,我得到的记录中r.new_type不为null。如何获取OptionSetValue r.new_type为空的记录?我相信下面这行:subtype=getAllOptionSetValuesr.LogicalName,new_type,r.new_type.Value!=无效的r、 当r.new_type.Value为null时,new_type.Value:defaultint失败,如果值为,则分配默认int似乎无法解决问题。我被卡住了,希望得到更多帮助。@IsiahGuantai您想要空记录和非空记录,还是只想要新类型为空的记录?我想要空记录和非空记录。其实我已经解决了这个问题。我更改了subtype=getAllOptionSetValuesr.LogicalName,new_type,r.new_type==null?defaultint:r.new_type.Value to subtype=r.GetFormattedAttributeValuenew_type,它工作得很好!这行代码又出现了一个空问题:actualrelevenue=decimalr.ActualValue.Value=null?小数。实际值。值:defaultdecimal。请帮忙!