LINQ is Language Integrated Query. In this blog, we will see how to use LINQ(Early Binding and Late Binding) in MS CRM.
LINQ operators are: from, join, where, orderBy, select, last, skip and take.
LINQ provider for MS CRM does not allow “aggregates” and “group by”
We need these clause to construct query.
In Late Binding use CreateQuery method:
- var query = from c in context.CreateQuery<Contact>()
- select c;
In Early Binding use generated service context:
- var query = from c in context.ContactSet
- select c;
1] Late Binding Example
Create new console application in visual studio File > New Project > Console Application.
Add Microsoft.Xrm.Sdk references.
Then add below code to your Program.cs file.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Crm.Sdk.Messages;
- using Microsoft.Xrm.Tooling.Connector;
- using System.Net;
- using System.ServiceModel;
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Sdk.Client;
- namespace LinqInCRM {
- class Program {
- static void Main(string[] args) {
- string url = "https://yourorg.crm.dynamics.com";
- string userName = "[email protected]";
- string password = "*****";
- string conn = $ @ "
- Url = {
- url
- };
- AuthType = Office365;
- UserName = {
- userName
- };
- Password = {
- password
- };
- RequireNewInstance = True ";
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
- using(var svc = new CrmServiceClient(conn)) {
- #
- region LateBinding
- OrganizationServiceContext orgContext = new OrganizationServiceContext(svc);
- var query = from c in orgContext.CreateQuery("contact")
- join a in orgContext.CreateQuery("account")
- on c["parentcustomerid"] equals a["accountid"]
- where(String) c["firstname"] == "Benno" ||
- (String) c["lastname"] == "Kurmann"
- select new {
- Contact = new {
- FirstName = c["firstname"],
- LastName = c["lastname"]
- },
- Account = new {
- Name = a["name"]
- }
- };
- foreach(var record in query) {
- Console.WriteLine("Contact Name: {0} {1}",
- record.Contact.FirstName, record.Contact.LastName);
- Console.WriteLine("Account Name: {0}",
- record.Account.Name);
- }#
- endregion
- Console.WriteLine("Press any key to exit.");
- Console.ReadLine();
- }
- }
- }
- }
2] Early Binding Example
First you need to generate service context using Early Bound Generator plugin in XRM toolbox
- Open XRMToolBox
- Connect to your organisation
- Open plugin Early Bound Generator
- In entities section – Entities Whitelist – select entities name that you have to generate service context, in global section set namespace
- Change service context name if you want
- Click Create All button
- Generated file path as below:
C:\Users\username\AppData\Roaming\MscrmTools\XrmToolBox\Settings\EBG\Actions.cs
C:\Users\ username\AppData\Roaming\MscrmTools\XrmToolBox\Settings\EBG\OptionSets.cs
C:\Users\ username\AppData\Roaming\MscrmTools\XrmToolBox\Settings\EBG\Entities.cs
- After creating the files copy them into your console application project
- Add namespace that you set in global section
- Add below code in Program.cs file – In this sample we have retrieve data from CRM and created record in crm
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Crm.Sdk.Messages;
- using Microsoft.Xrm.Tooling.Connector;
- using System.Net;
- using System.ServiceModel;
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Sdk.Client;
- using CrmEarlyBound;
- namespace LinqInCRM {
- class Program {
- static void Main(string[] args) {
- string url = "https://yourorg.crm.dynamics.com";
- string userName = "[email protected]";
- string password = "*****";
- string conn = $ @ "
- Url = {
- url
- };
- AuthType = Office365;
- UserName = {
- userName
- };
- Password = {
- password
- };
- RequireNewInstance = True ";
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
- using(var svc = new CrmServiceClient(conn)) {
- #
- region EarlyBinding
- using(var serviceContext = new CrmServiceContext(svc)) {
- var strLead = from ct in serviceContext.LeadSet
- orderby ct.FirstName descending
- where(int) ct.StateCode == 3
- select new {
- lead = new {
- fName = ct.FirstName,
- lName = ct.LastName,
- statusValue = (OptionSetValue) ct.StatusCode,
- statusName = ct.StatusCodeEnum,
- owner = (EntityReference) ct.OwnerId,
- descision = (bool) ct.DecisionMaker,
- amount = (Money) ct.BudgetAmount
- }
- };
- foreach(var row in strLead) {
- Console.WriteLine("First Name: {0} Last Name: {1} StatusCode: {2} StatusName: {3} OwnerID: {4} Owner: {5} Descision: {6} Money: {7} \n",
- row.lead.fName, row.lead.lName, row.lead.statusValue.Value, row.lead.statusName.ToString(), row.lead.owner.Id, row.lead.owner.Name,
- row.lead.descision, row.lead.amount.Value);
- }
- }#
- endregion
- Console.WriteLine("End of first query\n");#
- region Create lead
- Lead lead = new Lead() {
- FirstName = "Akash",
- LastName = "Vidhate"
- };
- Guid leadid = svc.Create(lead);
- Console.WriteLine("Lead created lead Guid is: " + leadid);#
- endregion
- Console.WriteLine("Press any key to exit.");
- Console.ReadLine();
- }
- }
- }
- }
Now you can debug the code check the result.