OrganizationServiceContext.CreateQuery vs Fetch
Asked Answered
G

1

6

Hoping someone could shed some light from a performance standpoint on using OrganizationServiceContext.CreateQuery vs using FetchXML (or QueryExpression).

I have made extensive use of LINQ but am new to CRM. CreateQuery seems like a good fit for my skill set but I wonder about performance in the end.

I realize that straight up

var result = from e in orgContext.CreateQuery("xyz_myentity")
             where e["email"] == "[email protected]"
             select e;

will return all attributes for xyz_myentity, but I can't seem to find any documentation for the LINQ provider that sits on top of CRM. Will the using an anonymous type limit attributes returned from SQL / CRM? Or is the magic being done "client" side after the full set of data is returned from the server? Is the SQL query for all attributes and then is the LINQ provider building the anonymous type on top of that?

var result = from e in orgContext.CreateQuery("xyz_myentity")
             where e["email"] == "[email protected]"
             select new { Name=e["xyz_name"] };

Are there any other considerations related to the introduction of the OrganizationServiceContext?

Gideon answered 7/1, 2016 at 23:21 Comment(0)
N
7

LINQ for CRM is built on top of the QueryExpression and therefore has pretty much the same limitations. It is implemented client-side in the Microsoft.Xrm.Sdk library. The library sends QueryExpression requests to the CRM web service.

LINQ adds an abstraction layer to the query stack (LINQ -> QueryExpression -> SQL), so you can expect a small performance hit.

FetchXML is the oldest technique available for querying CRM. Microsoft states it is slightly less performant on the server. Writing FetchXml queries comes down to constructing XML messages, so in general the programming experience is poor.

The OrganizationServiceContext is required for processing LINQ queries; it is an object capable of tracking changes to data objects. It is known to have some issues and, again, it comes with a cost. E.g. tracking large amounts of data is not advisable.

When I have to choose between LINQ and QueryExpression I still favour the latter. It's the most light-weight technology when you need to query CRM and with the help of a few extension methods the programming experience is OK.

(Note: CRM 2016 adds a new querying method using Web API.)

Nomo answered 8/1, 2016 at 6:27 Comment(3)
Thanks for the info. Any additional references you can point me to?Gideon
Here's a nice comparison between query options: blogs.msdn.microsoft.com/crminthefield/2013/01/14/…Nomo
Another article about conversion of LINQ queries to QueryExpressions can be found here: code.msdn.microsoft.com/Convert-CRM2011-Linq-into-93a163ee.Nomo

© 2022 - 2024 — McMap. All rights reserved.