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 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)

  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 )
    +1

  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 :o)

  4. 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…

Share your thoughts

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s