QueryExpression 与 FetchXml CRM2011

发布于 2025-01-03 13:32:29 字数 538 浏览 0 评论 0原文

我们发现 Linq for CRM 2011 严重损坏 - 它似乎在没有对其进行任何 QA 的情况下就进入了。指示提供程序损坏程度的指标是像 .Where(x => x== "b") 这样的查询可以工作,但是这个 .Where(x => "b" == x) 可能不依赖于某些前面的条件就像一个 join 语句。实际上,我不得不重写查询提供程序的部分内容,并且我对我整理的垃圾感到幸运。

然而,这种情况不能继续下去,仍然存在其他问题,而且我没有报酬为 MS 工作,所以我正在寻找替代方案。这两个出现了 QueryExpression & FetchXml 详细信息如下: http://msdn.microsoft.com/en-us/ library/gg334607.aspx

谁能给我一个诚实的、现实生活中使用 QueryExpression 与 FetchXml 的优缺点?我想知道它们在性能、开发速度、稳健性和灵活性方面的比较如何。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

摇划花蜜的午后 2025-01-10 13:32:29

为了以 Anwar 重点关注 LINQ 与 FetchXml 的优秀答案为基础,我将补充一点:我从不使用 QueryExpression为什么

LINQ:查询是使用标准语言构建的,但内部使用
QueryExpression 因此仅限于 QueryExpression 的功能。

QueryExpression:查询被构建为对象模型。支持所有
FetchXML 中除聚合和分组之外的功能。

因此,它的查询能力比没有高级查找代码生成的 FetchXml 差,并且它提供与 LINQ 提供程序相同的功能,同时提供完全非标准的查询接口(与LINQ)。

至于 LINQ(非)功能,LINQ 提供程序的限制很明显,而且我认为相当不错,已记录。例如,您的 .Where(x => "b" == x) 代码段违反了 where 子句限制:

where:子句的左侧必须是属性名称,右侧必须是属性名称
子句的一侧必须是一个值。您不能将左侧设置为
持续的。子句两边不能都是常量。

不是为 Microsoft 辩护:在 LINQ 提供程序达到专业级之前,他们需要在 LINQ 提供程序(阅读:直接到 SQL 提供程序)上投入大量工作,但是嘿,至少他们有一个很好的免责声明。

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.

注定孤独终老 2025-01-10 13:32:29

在我看来,我通常根据需求选择 Linq 或 FetchXml。

对于 Linq:对于早期绑定,我喜欢使用 Linq,因为它是强类型的,并且它对开发速度有很大帮助,但正如您上面所说,它有其缺点。

对于 FetchXML:我真的很喜欢使用这个神奇的语句:

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

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

为什么?因为除了聚合分组之外,它与使用 QueryExpression 非常相似。
我唯一讨厌 FetxhXML 的地方是它很难构建,这与 Linq 不同。

为了构建 FetchXML 查询,我必须打开 Advanced-Find,然后添加列,然后输入我的条件等等,最后我下载它并将其复制到我的代码中,等等。

最后,FetchXML 在其他方法中具有最少的限制。

关于性能,我尝试使用 StopWatch 在 Linq 和 FetchXML 之间对同一查询进行基准测试,结果是 FetchXML 比 Linq 更快。

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.

放飞的风筝 2025-01-10 13:32:29

客户特别要求我使用查询表达式模型,因此为了让我的生活更轻松,我向 IOrganizationService 添加了很多扩展方法。示例包括:

public static List<T> GetEntities<T>(
    this IOrganizationService service, 
    params object[] columnNameAndValuePairs
) where T : Entity

将params object[]和T实体类型转换为查询表达式,并自动将结果返回到实体列表。所以它是这样使用的:

foreach(var c in service.GetEntities<Contact>("lastname", "Doe", "firstname", "Smith"))
{
    ... 
}

我也经常使用这个:

public static T GetFirstOrDefault<T>(
    this IOrganizationService service,
    params object[] columnNameAndValuePairs
) where T : Entity

var c = service.GetFirstOrDefault<Contact>("owner", id);

这些类型的扩展方法使查询表达式的使用变得更加容易,为您提供了更多的 LINQ 类型风格,而没有容易陷入的奇怪的 linq 限制陷阱。

I've been asked specifically by a client to use the Query Expression model, so in order to make my life easier, I've resorted to adding a lot of extension methods to IOrganizationService. Examples Include:

public static List<T> GetEntities<T>(
    this IOrganizationService service, 
    params object[] columnNameAndValuePairs
) where T : Entity

which converts the params object[] and T entity type into a query expression, and automatically returns the results to an entity list. So it is used like so:

foreach(var c in service.GetEntities<Contact>("lastname", "Doe", "firstname", "Smith"))
{
    ... 
}

I also use this one a lot too:

public static T GetFirstOrDefault<T>(
    this IOrganizationService service,
    params object[] columnNameAndValuePairs
) where T : Entity

var c = service.GetFirstOrDefault<Contact>("owner", id);

These type of extension methods make working with query expressions a lot easier, giving you a much more LINQ type style, without weird linq restriction traps that are easy to fall into.

南笙 2025-01-10 13:32:29

我提倡使用 FetchXML,因为我可以在 JavaScript 或 C# 代码中使用它,这与 LINQ 或 QueryExpression 不同......因此少了一件需要学习和维护的事情。至于像 Intellisense 这样的东西,有一个很棒的工具可以插入到 XrmToolbox 中,名为 FetchXML Builder,它在以下方面要复杂得多:设计比使用“高级查找”所见过的复杂查询。我已经在 CRM Online 客户端上使用它一个月了,它与在此环境中使用 SQL 非常接近。它还可以为我生成 QueryExpression 代码。我已将这个工具移交给我的业务分析师,他们将使用它为仪表板制作复杂的数据集 - 这对客户来说是一个巨大的胜利。

我确实对早期绑定无法检测错误感到遗憾,但我喜欢

I would advocate in favor of FetchXML because I can use it in my JavaScript or C# code, unlike LINQ or QueryExpression...therefore one less thing to learn and maintain. As for stuff like Intellisense, there is a great tool that plugs into XrmToolbox called FetchXML Builder that is much more sophisticated in designing complex queries than you will ever see using Advanced Find. I have been using it now for a month for a CRM Online client and it is as close to using SQL as you can get in this environment. It can also generate QueryExpression code for me. I have turned over this tool to my business analysts and they are going to town using it to make sophisticated data sets for the dashboard - a big win for the clients.

I do lament the loss of detecting errors with early binding, but I enjoy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文