CRM 2011 – Early bound LINQ queries

I have been trying to get my head around LINK and OData today.  OData was really giving me the run around, trying to bring the sample into a piece of Javascript was very frustrating.

I am a big fan of Early bound data in CRM 2011, I just like to know what I am using rather than the dynamic late binding, strong typing means you catch any problems in the code when compiling it and saves you a bunch of time debugging misspelt entites in strings.

I couldn’t figure out how to get the results of a Linq query in a early bound entity.  In link you often just use the Var variable and then LINQ converts the results into the correct type.

The SDK also didn’t seem to have any examples but I had been reading about LINQ for the 70-515 .NET web apps exam.  I knew the result had to be IEnumerable so you can go through the results.

The quick answer is you have use this


instead of a


here is a snippet of my code.  if anyone can tell me the easiest way to display code in WordPress that would be appreciate, please leave me some advice in the comments.  This blog post has some good information on LINQ queries

_service = (IOrganizationService)_serviceProxy;

OrganizationServiceContext orgContext =  new OrganizationServiceContext(_service);

IEnumerable<Account> accounts = from a in orgContext.CreateQuery<Account>()

select new Account


Name = a.Name,
Address1_County = a.Address1_County

System.Console.WriteLine("List all accounts in CRM");                    
 foreach (Account a in accounts) {                        
System.Console.WriteLine(a.Name + " " + a.Address1_County);                    

System.Console.WriteLine("<End of Listing>");                    

8 thoughts on “CRM 2011 – Early bound LINQ queries

  1. Stuart McLeod April 26, 2011 / 1:45 am

    That works well for accounts and contacts, but what about SalesOrders and the likes?

    IEnumerable salesOrders = from so in ctx.CreateQuery()
    select new SalesOrder
    TotalAmount > 0

    gives a Cannot initialize type ‘SalesOrder’ with a collection initializer because ti does not implement ‘System.Collections.IEnumerable’

    Seems ridiculous! Any assistance would be greatly appreciated.


  2. Stuart McLeod April 26, 2011 / 1:46 am

    Sorry. That code should have been:

    IEnumerable salesOrders = from so in ctx.CreateQuery()
    select new SalesOrder
    TotalAmount > 0


    • Hosk April 26, 2011 / 4:44 pm

      I did this and it worked for me, I initially had problems with the Total amount because it was a money.

      hope it helps

      public void searchSalesOrder(Microsoft.Xrm.Sdk.Client.OrganizationServiceContext orgContext, string candidateId)
      IEnumerable salesOrders = from c in orgContext.CreateQuery()
      where (c.TotalAmount.Value > 0)
      select c;;

      foreach (SalesOrder a in salesOrders)
      Console.WriteLine(“Name: ” + a.TotalAmount);

      catch (FaultException)
      // You can handle an exception here or pass it back to the calling method.


  3. Stuart McLeod April 27, 2011 / 11:03 am

    Turns out my issue was being caused by the killing off of CRMDateTime. But thanks for the assistance. I’ve used that code now!


  4. Stuart McLeod April 28, 2011 / 7:23 am

    Hi Ben,

    Sorry to hassle you, but I thought you might know the answer to this…

    In CRM4 when you bought back say an Account it would have all it’s relations. For instance, again in my SalesOrder example I could go

    Account a = salesOrder.order_customer_accounts;

    But now, salesOrder.order_customer_accounts is null.

    Any idea how to bring back an entity’s related entities?




  5. Brent June 2, 2011 / 6:22 pm

    Hello Ben,

    My question is the same as Stuart’s above. Essentially how to bring back an entities related entities. We are doing this from a Silverlight form and when a contact is selected we want to grab the contact name from that entity and related items for that contact from a custom entity.

    Thank you,


    • Hosk July 31, 2015 / 10:12 am

      My plugin can’t use plugins because it’s a free blog.

      But I have found a way to format the code and have tidied it up for you (and me)

      ps make sure you are reading my latest blogs and not just 4 year old blog posts


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.