The following are the different ways of querying CRM data:

  • Fetch XML supports aggregates and grouping
  • LINQ to query CRM data no support for aggregates and grouping.

 A Sample code querying an entity name Airport in Dyanmics CRM 2011

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel.Description;
using System.Net;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
namespace CRMDataQuery
{
class Program
{
static void Main(string[] args)
{
//Establish connection to the web serivce end point

Uri orgUri = new Uri(“http://crm:5555/ACM/XRMServices/2011/Organization.svc”);
Uri homerealUri = null;

ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
try
{
//Instantiate a new instance of CRM OrgranizationServiceProxy class
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(orgUri, homerealUri, credentials, null))
{
//Ensure support for early-bound types
serviceProxy.EnableProxyTypes();

IOrganizationService service = (IOrganizationService)serviceProxy;

//Create the Context object that will provide IQuerable Collection

OrganizationServiceContext context = new OrganizationServiceContext(service);

var airports = (from f in
context.CreateQuery<acm_airport>()
select new
{
AirportCode = f.acm_name,
AirportName = f.acm_AirportName
});
foreach (var airport in airports)
{
Console.WriteLine(“====================== Airport Information ===================”);
Console.WriteLine(“AirportCode: {0}”, airport.AirportCode);
Console.WriteLine(Environment.NewLine);
Console.WriteLine(“AirportName: {0}”, airport.AirportName);
}
}
}
catch (Exception ex)
{
throw ex;
}
Console.ReadLine();
}
}
}