Microsoft Dynamics 365 : Using Organization service to Query data with the FetchExpression / QueryExpression / QueryByAttribute / LINQ methods

The SDK assemblies for the Organization service provide several methods to query data. Each method provides different advantages as shown below.

 

FetchExpression Method 

We use the proprietary FetchXML query language to create complex queries that return aggregates such as the sum of a value for all returned records. You can also perform group by operations with FetchXML. Can include data from linked table rows (entity records).

FetchXml is a proprietary XML-based query language that can be used with SDK Assembly queries using FetchExpression and by the Web API using the fetchXml query string.

The following example shows a simple query to return up to 50 matching account rows where the address1_city value equals Redmond, ordered by name:

 

 

QueryExpression Method

The QueryExpression class provides a strongly typed set of objects that is optimized for run-time manipulation of queries. Queries can also be created using FetchXML, you can convert queries between FetchXML and QueryExpression using FetchXmlToQueryExpressionRequest and QueryExpressionToFetchXmlRequest messages.

QueryExpression is useful in scenarios where you want to return multiple entities that match a certain criteria. It lets you specify which fields you want to have returned (from the specified entity type or any related entity) as part of the query result in order to improve performance. Developers can also control the order in which records are returned.

The following example shows a simple query to return up to 50 matching account rows where the address1_city value equals Redmond, ordered by name:

 

 

QueryByAttribute Method

The QueryByAttribute class provides a strongly typed set of objects that are optimized for simple, common queries of table rows. Unlike FetchXML and QueryExpression , QueryByAttribute can only return data from a single table (entity type). It doesn’t enable retrieving data from related table rows or complex query criteria. Use QueryByAttribute for queries where you are testing whether all the table column (attribute) value criteria in your query are a match.

The following example shows a simple query to return up to 50 matching account rows where the address1_city value equals Redmond, ordered by name:

 

 

LINQ Method 

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have to learn a different query language for each type of data source: SQL databases, XML documents, various Web services, and so on.

With LINQ, a query is a first-class language construct, just like classes, methods, events. You write queries against strongly typed collections of objects by using language keywords and familiar operators. The LINQ family of technologies provides a consistent query experience for objects (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).

For a developer who writes queries, the most visible “language-integrated” part of LINQ is the query expression. Query expressions are written in a declarative query syntax. By using query syntax, you can perform filtering, ordering, and grouping operations on data sources with a minimum of code. You use the same basic query expression patterns to query and transform data in SQL databases, ADO.NET Datasets, XML documents and streams, and .NET collections.

You can write LINQ queries in C# for SQL Server databases, XML documents, ADO.NET Datasets, and any collection of objects that supports IEnumerable or the generic IEnumerable<T> interface. LINQ support is also provided by third parties for many Web services and other database implementations.

The following code shows a simple example of the complete query operation. The complete operation includes creating a data source, defining the query expression, and executing the query in a foreach statement :