Parts of the Query

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

This section describes each valid clause in a Microsoft Dynamics CRM LINQ query.

from and join Clauses

The first step in constructing a query is identifying the relevant entity types and the relationships between them. The XrmDataContext class exposes IQueryable entity set properties to provide the entry point bindings against the Microsoft Dynamics CRM entities. These properties are used by the from and join statements of the LINQ query. A valid binding starts with a single from clause. Optionally, one or more join clauses can be appended to the chain and related to any previously bound entity. When joining multiple entities, the inner and outer key selectors of the on clause should specify primary key or foreign key (uniqueidentifier) properties of the entities. These are the properties with the "id" suffix and are Guid types.

Multiple from clauses are not valid within a single Microsoft Dynamics CRM LINQ query so a query with select-many behavior should be separated into individual queries. Consequently, all entities within the query have some sort of relationship.

The from clause plays a special role in Microsoft Dynamics CRM LINQ queries due to the structure of the underlying Microsoft Dynamics CRM query provider. The provider is only capable of returning entities of a single entity type. In other words, the select clause cannot combine properties from multiple entities. The from clause specifies the single "root" entity that is designated for projection. As a result, the orderby and select clauses must refer back to this root entity. Keep this in mind when deciding how to join multiple entities.

where Clause

In order to filter the result set, where clauses can be added against one or more of the entities. Each where clause may only contain conditions against an individual entity type. A composite condition involving multiple entities is not valid. Instead, each entity should be filtered in separate where clauses.

Where clauses support the standard set of Boolean operators (==, !=, >, >=, <, <=, &&, ||) along with a limited set of method calls (Equals, Contains, StartsWith, and EndsWith). The string-based method calls (Contains, Starts, EndsWith) translate into the "like" condition in FetchXML, which is a case-insensitive operation. As a result, there is an implicit StringComparison.InvariantCultureIgnoreCase IEqualityComparer used with these method calls.

orderby Clause

Query results can be sorted by a property of the entity specified in the from clause. Sorting on an entity specified by a join clause is not supported. This is related to the constraint that a query may only project a single entity type.

The following example shows how to use the orderby clause.

var descendingMarketingLists =
   from marketingList in crm.lists
   orderby marketingList.listname descending
   select marketingList;

select Clause

The main rule to keep in mind when mapping properties is that only properties of the entity specified in the from clause may be referenced. Otherwise, the Microsoft Dynamics CRM LINQ queries support a fairly rich set of mapping capabilities.

The following example shows how to use the select clause.

// Retrieve one property.
var marketingListNames =
   from marketingList in crm.lists
   select marketingList.listname;

// Instantiate an object.
var marketingListsAsStaticType =
   from marketingList in crm.lists
   select new KeyValuePair<Guid, string>(marketingList.listid,

// Assign the entity to an alias.
var marketingListsAsAlias =
   from marketingList in crm.lists
   select new { list = marketingList };

// Assign to an anonymous type.
var marketingListsAsAnonymousType =
   from marketingList in crm.lists
   select new { marketingList.listname, ID = marketingList.listid };

// Perform a mapping calculation.
var marketingListsWithCalculations =
   from marketingList in crm.lists
   select new { marketingList.listname, Cost = marketingList.cost + 100 };

See Also


© 2010 Microsoft Corporation. All rights reserved.