Note that there are some explanatory texts on larger screens.

plurals
  1. POLinqToCRM does not cast properly
    text
    copied!<p>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).</p> <pre><code> var context = new OrganizationServiceContext(crmService); SystemUser x = (from c in context.CreateQuery&lt;SystemUser&gt;() where c.DomainName == @"pfgc\" + Environment.UserName select c).FirstOrDefault(); </code></pre> <p>This code is straightforward. I've even tried without the Where clause and it won't change anything.</p> <p>I tried the following (no FirstOrDefault and var instead of SystemUser)</p> <pre><code> var x = (from c in context.CreateQuery&lt;SystemUser&gt;() where c.DomainName == @"pfgc\" + Environment.UserName select c); </code></pre> <p>This won't throw an exception but x type is Microsoft.xrm.sdk.linq.Query. What am I doing wrong? It seems to be exactly what the SDK suggests to do.</p> <p><strong>EDIT:</strong> </p> <p>GCATNM has the right answer. In case someone faces the same issue, here's a sample of the working code:</p> <pre><code> public SystemUser GetCurrentUser() { var context = GetOrgContext(); return (from c in context.CreateQuery&lt;SystemUser&gt;() where c.DomainName == @"pfgc\" + Environment.UserName select c).FirstOrDefault(); } public OrganizationServiceContext GetOrgContext() { var serviceProxy1 = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null); serviceProxy1.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior()); return new OrganizationServiceContext(serviceProxy1); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload