Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4098274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:16:50+00:00 2026-05-20T20:16:50+00:00

I want to change all my queries from QueryExpression to Linq. In development time,

  • 0

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. What am I doing wrong? It seems to be exactly what the SDK suggests to do.

EDIT:

GCATNM has the right answer. In case someone faces the same issue, here’s a sample of the working code:

    public SystemUser GetCurrentUser()
    {
        var context = GetOrgContext();
        return (from c in context.CreateQuery<SystemUser>()
                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);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:16:51+00:00Added an answer on May 20, 2026 at 8:16 pm

    I had a similar problem with LINQ-to-CRM and went back to QueryExpressions because they worked, until I found the solution in some SDK sample while looking for something else: You need to add a ProxyTypesBehavior to your IOrganizationService object. I don’t know what it does, but that definitely was the change that allowed me to use LINQ with the early bound classes (as I perceive it, LINQ-to-CRM can only be used with the early bound classes).

    So the line you need after creating your IOrganizationService is:

    organizationService.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
    

    I hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to redirect all GET queries from one site to another, using
I want only registered users to vote. I do not want to change all
I want to use javascript to change all of the <option> tags to <a>
Let's say I want to write a regular expression to change all <abc> ,
Hey all, I want to change a value in a table (in a mysql
All I want is to be able to change the color of a bullet
I want to register to get notified of all Java changes in Eclipse. I
I want to see a list of all changes the next push would do.
All I want to do is to check if the textfield has been changed.
i want change the content of a UITableView when i change a button, how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.