Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QueryExpression vs. FetchXml CRM2011

We found out that Linq for CRM 2011 is horribly broken - it seems to have gotten in without any QA performed on it. An indicator as how badly broken the provider is a query like .Where(x => x== "b") works but this .Where(x => "b" == x) might not depending on some preceding condition like a join statement. I have actually had to rewrite parts of the query provider and am enjoying better luck with the crap I put together.

However this can't go on, there are still other issues and I'm not paid to to work for MS, so I'm looking at alternatives. These 2 came up QueryExpression & FetchXml as detailed here: http://msdn.microsoft.com/en-us/library/gg334607.aspx

Can anyone give me an honest, real life pros and cons of using QueryExpression vs. FetchXml? I would like to know how they compare in terms of performance, development speed, robustness and flexibility.

like image 520
Alwyn Avatar asked Feb 07 '12 19:02

Alwyn


People also ask

What is the difference between FetchXML and QueryExpression?

The main difference is that fetchXML support aggregation whether query expression does not support aggregation. Secondly we will use Query expression when our query is complex and its server side coding , where as fetch XML is easy to build and easy to use and you can write both server side and client side as well.

What are the limitations of FetchXML?

Example FetchXML query strings A FetchXML query has a limit of a maximum of 15 allowed link tables.


2 Answers

To build on Anwar's excellent answer focusing on LINQ vs. FetchXml, I'll add I never use QueryExpression. Why?

LINQ: Queries are built using standard language, but internally uses QueryExpression so is limited to the features of QueryExpression.

QueryExpression: Queries are built as an object model. Supports all the features in FetchXML except for aggregates and grouping.

So it's worse in querying power than FetchXml without the Advanced Find code generation, and it offers the same functionality as the LINQ provider while offering a completely non-standard querying interface (unlike LINQ).

As for LINQ (non)functionality, the limitations of the LINQ provider are clearly, and I think fairly well, documented. Your .Where(x => "b" == x) snippet, for instance, violates the where clause restriction:

where: The left side of the clause must be an attribute name and the right side of the clause must be a value. You cannot set the left side to a constant. Both the sides of the clause cannot be constants.

Not defending Microsoft: they need to put in a lot of work on the LINQ provider (read: direct-to-SQL provider) before the LINQ provider is professional grade, but hey, at least they've got a great disclaimer.

like image 133
Peter Majeed Avatar answered Oct 20 '22 16:10

Peter Majeed


In my opinion, I usually go for Linq or FetchXml depending for the requirements.

For the Linq: In case for early-bound, I like using Linq because it's strongly typed and It helps much for development speed, but as you stated above, it has its disadvantages.

For the FetchXML: I really love using this magic statement:

EntityCollection result = _serviceProxy.RetrieveMultiple(new FetchExpression(fetch2));

foreach (var c in result.Entities)
{
   System.Console.WriteLine(c.Attributes["name"]);
}

Why? Because it's very similar of using the QueryExpression in addition of the aggregation and grouping. The only thing I hate about FetxhXML is that it's hard to build, unlike the Linq.

For building FetchXML queries, I have to open the Advanced-Find then add columns then put my criteria and so on, finally I download it and copy it into my code, and so on.

Finally, the FetchXML has the least limitations among the others.

Regarding the performance I've tried to benchmark between Linq and FetchXML for same query using StopWatch, the result was FetchXML is faster than the Linq.

like image 28
Anwar Avatar answered Oct 20 '22 16:10

Anwar