Linq Query CRM 2011 (late bound)


Add references to the following three dlls in the project

The initialization code

Uri organizationUri = new Uri("http://CRM2011/Contoso/XRMServices/2011/Organization.svc");
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential("administrator", "pass@word2", "contoso");
OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
// Get the IOrganizationService
IOrganizationService orgService = (IOrganizationService)orgProxy;
//Get OrganizationServiceContext -the organization service context class implements the IQueryable interface and
//a .NET Language-Integrated Query (LINQ) query provider so we can write LINQ queries against Microsoft Dynamics CRM data.
OrganizationServiceContext orgServiceContext = new OrganizationServiceContext(orgService);

1) Simple Select

// Get name,number and ownerid for all the account records
var queryAccount1 = from r in orgServiceContext.CreateQuery("account")
select new
{
AccountName = r["name"],
AccountNumber = r["accountnumber"],
Owner = (EntityReference)r["ownerid"]
};

foreach (var account in queryAccount1)
{
MessageBox.Show("Account Name :-" + account.AccountName.ToString() + " " +
" Account Number :-" + account.AccountNumber.ToString() +
" Account Owner Id :- " + account.Owner.Id.ToString());
}

2)Select with Where

var queryAccount2 = from r in orgServiceContext.CreateQuery("account")
where ((EntityReference)r["accountid"]).Id.Equals(new Guid("EFB29802-7283-E011-8259-00155D045710"))
select new
{
AccountName = r["name"],
AccountNumber = r["accountnumber"],
Owner = (EntityReference)r["ownerid"]
};

3) Select with 2 Where clause

var queryAccount3 = from r in orgServiceContext.CreateQuery("account")
where (r["name"].Equals("test account 2") && r["address1_postalcode"].Equals("382424"))
select new
{
AccountName = r["name"],
AccountNumber = r["accountnumber"],
Owner = (EntityReference)r["ownerid"]
};

4) Where operator supports the following String functions Contains, StartsWith, EndsWith, and Equals.

var queryContactwithFirstNameasNishant = from r in orgServiceContext.CreateQuery("contact")
where ((string)r["firstname"]).Contains("Nishant")
select new
{
FirstName = r["firstname"],
LastName = r["lastname"]
};

var queryContactStartsWithN = from r in orgServiceContext.CreateQuery("contact")
where ((string)r["firstname"]).StartsWith("N")
select new
{
FirstName = r["firstname"],
LastName = r["lastname"]
};

8 thoughts on “Linq Query CRM 2011 (late bound)”

  1. Hi i need to retrive the data from the employees extension into the project report extension’s forms text field, means i want to retrive data of one entity into another and then save it. please help me…

    Like

  2. Hi Nishant. Sounds like great stuff. But when I try to include attributes that are not set to required, the linq query just do not seem to work when a single record with a null value attribute is in the result. Is there a way to coop with this behavior? (btw: I am not a programmer :o)

    Like

  3. Hi Nishant. Sounds like great stuff. But when I try to include attributes that are not set to required, the linq query just do not seem to work when a single record with a null value attribute is in the result. Is there a way to coop with this behavior? (btw: I am not a programmer )
    +1

    Like

  4. Hi Nishant sir,

    I am trying to access data from crm in siverlight through Odata Service.
    I want to retrive account info on the basis of contact name.
    for the same purpose I used join in my query.
    While executing at “_account.LoadAsync(qery2);” I get error that Join is not supported.

    My question might be silly but I would be too grateful to you If you could help me out.

    Thanks in advance

    code snippet

    var qery2 = from a in context.AccountSet
    join c in context.ContactSet on a.PrimaryContactId.Id equals c.ContactId
    where c.FullName.Contains(“some_name”)
    select a;

    account.LoadCompleted += new EventHandler _account_LoadCompleted_1);

    _account.LoadAsync(qery2)

    Like

Please share your thoughts

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

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM