Bug: Can't query for properties of a contract.statuscode

Oct 16, 2009 at 6:40 PM

Unfortunately I haven't figured out how to get the Fetch XML for this, but the following query results in a "Failed to project selection" exception.

var contracts = (from Contract in crm.Linq<contract>()
                             //join Account in crm.Linq<account>()
                             //on Contract.ownerid.Value equals Account.accountid.Value
                             //where Account.name == account.AccountName
                             where Contract.customerid.Value == account.AccountGuid
                             select new {
                                 contractGuid = Contract.contractid.Value,
                                 accountGuid = Contract.ownerid.Value,
                                 startDate = Contract.activeon.date,//Start date for contract
                                 endDate = Contract.expireson.date,//End date for contract
                                 state = Contract.statuscode.name
                             }).ToArray();

 

If I change Contract.statuscode.name to just Contract.statuscode the query works fine. I have also tried Contract.statuscode.Value, however that causes the same exception.

Full stack trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   lambda_method(ExecutionScope ) +1129

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +0
   System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +71
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +350
   System.Delegate.DynamicInvokeImpl(Object[] args) +84
   System.Delegate.DynamicInvoke(Object[] args) +7
   LinqtoCRM.d__4`1.MoveNext() +691

[Exception: Failed to project selection.]
   LinqtoCRM.d__4`1.MoveNext() +805
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +259
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +81
   Crmunicator.Controllers.PreloadController.getCrmContracts(DataCrmAccount account, CrmDataContext db) in C:\Infrastructure\CRM\Crmunicator\Crmunicator\Controllers\PreloadController.cs:153
   Crmunicator.Controllers.PreloadController.Index(String id) in C:\Infrastructure\CRM\Crmunicator\Crmunicator\Controllers\PreloadController.cs:39
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +140
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.b__7() +52
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassc.b__9() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399
   System.Web.Mvc.Controller.ExecuteCore() +126
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

Developer
Oct 19, 2009 at 7:55 AM

What kind of an object is the account object? Hard to test exactly your situation when your code does not tell.

 

Developer
Oct 19, 2009 at 8:08 AM
        [Test]
        public void TestLookupName()
        {
            var contacts = from c in provider.Linq<contact>() select new { ModifiedBy = c.modifiedby.name };
            Assert.AreEqual("Administrator", contacts.AsEnumerable().First().ModifiedBy);
        }

This simple test I just added works at least. Check first that your database doesn't return null values for the status codes. Nulls need to be handled in the projection like attr = (a.attr == null ? false : a.attr.Value).

 

Oct 19, 2009 at 12:01 PM

Praty,

 

I believe I have located the issue. This started popping up in other areas of code sporadically, and it seems it is caused by fields having null values in CRM. If only our data was consistent. Somehow in the set of contracts I was querying for there was a contract without a statuscode set. Sorry to bother you.

 

Thanks

Tim