Cast issue with early bound Linq to CRM RRS feed

  • Question

  • Hi,

    I want to change all my queries from QueryExpression to Linq. In development time, all seems to be just fine, but I always get a cast exception at runtime (can't cast Microsoft.xrm.sdk.entity to Xrm.SystemUser -> Xrm is the early bound classes generated with CrmSvcUtil).

          var context = new OrganizationServiceContext(crmService);
          SystemUser x = (from c in context.CreateQuery<SystemUser>()
              where c.DomainName == @"pfgc\" + Environment.UserName
              select c).FirstOrDefault();

    This code is straightforward. I've even tried without the Where clause and it won't change anything.

    I tried the following (no FirstOrDefault and var instead of SystemUser)

          var x = (from c in context.CreateQuery<SystemUser>()
              where c.DomainName == @"pfgc\" + Environment.UserName
              select c);
    This won't throw an exception but x type is Microsoft.xrm.sdk.linq.Query<Xrm.SystemUser>. What am I doing wrong? It seems to be exactly what the SDK suggests to do.

    Sunday, March 20, 2011 3:42 PM

All replies

  • Am I the only one to have that issue? If so, I'll have to check somewhere else in my code.

    Monday, March 21, 2011 12:44 PM
  • moving to development

    Regards, Donna

    Tuesday, March 22, 2011 5:16 PM
  • I don't have a CRM2011 instance to play with at the moment, but try:

    Xrm.SystemUser x = 
    		from c in context.CreateQuery<Xrm.SystemUser>()
    		where c.DomainName == @"pfgc\" + Environment.UserName
    		select c

    --pogo (pat)
    Tuesday, March 22, 2011 11:57 PM
  • do you have:


    crmService.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
    somewhere? this is required in order to use early binding


    Thank you in advance for your help. If you think you may be able to help with any of my unanswered threads please look at them here
    Wednesday, March 23, 2011 4:07 PM