Using the late bound method, here is the same query results using QueryByAttribute, FetchXML and LINQ. For this example, I wrote a simple Console Application in Visual Studio 2012.
- Create a new project and choose Console Application template.
- In Solution Explorer, expand the References folder and add the Microsoft.Xrm.Sdk.dll and Microsoft.Xrm.Client.dll from the CRM SDK /bin folder
- Also add the System.Runtime.Serialization Assembly
- Your References should look similar to this:
- In the code for Program.cs the using statements need to include the following:
- Inside the Main method, the CrmConnection class is used to connect to Dynamics CRM.
- Use the OrganizationService and OrganizationServiceContext classes to setup the service and orgContext objects.
- Code for the query expression (QueryByAttribute)
Console.WriteLine(“==QueryByAttribute==”); QueryByAttribute qba = new QueryByAttribute(“account”);
qba.ColumnSet = new ColumnSet(“name”);
qba.Attributes.AddRange(“name”);
qba.Values.AddRange(“ACME”);
EntityCollection ecqba = service.RetrieveMultiple(qba);
foreach (Entity acct in ecqba.Entities)
{
Console.WriteLine(acct.Attributes[“name”]);
}
- Code for LINQCode for FetchXML
Console.WriteLine(“==FetchXML==”); string fetch = @”
<fetch mapping=’logical’>
<entity name=’account’>
<attribute name=’name’/>
<filter type=’and’>
<condition attribute=’name’ operator=’eq’ value=’ACME’ />
</filter>
</entity>
</fetch>”;
EntityCollection ecf = service.RetrieveMultiple(new FetchExpression(fetch));
foreach (Entity acct in ecf.Entities)
{
Console.WriteLine(acct.Attributes[“name”]);
}
Reblogged this on Dinesh Ram Kali..
LikeLike