在 CRM 2011 中使用 FetchXML

发布于 2024-12-06 08:21:38 字数 1249 浏览 0 评论 0原文

我正在使用 FetchXML,并且对其中两个实体进行分组和使用计数,但我不需要对其余实体进行分组,我只需要下拉数据。例如,这是我的代码:

string groupby1 = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
 <entity name='opportunity'>
  <attribute name='name' alias='opportunity_count' aggregate='countcolumn' />  
  <attribute name='ownerid' alias='ownerid' groupby='true' />
  <attribute name='createdon' alias='createdon' /> 
  <attribute name='customerid' alias='customerid' /> 
 </entity> 
</fetch>";

EntityCollection groupby1_result = orgProxy.RetrieveMultiple(new FetchExpression(groupby1));

foreach (var c in groupby1_result.Entities)
{
  Int32 count = (Int32)((AliasedValue)c["opportunity_count"]).Value;
  string OwnerID = ((EntityReference)((AliasedValue)c["ownerid"]).Value).Name;
  DateTime dtCreatedOn = ((DateTime)((AliasedValue)c["createdon"]).Value);
  string CustomerName = ((EntityReference)((AliasedValue)c["customerid"]).Value).Name;
}

但我收到此错误:

异常:System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]:当已指定聚合操作且既不是 groupby 也不是聚合时,无法请求属性。 NodeXml :(故障详细信息等于 Microsoft.Xrm.Sdk.OrganizationServiceFault)。

如何对某些值而不是其他值使用聚合?

I am using FetchXML and I am grouping and using count for two of the entities but the rest of the entities I don't need grouped I just need the data to be pulled down. For example this is my code:

string groupby1 = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
 <entity name='opportunity'>
  <attribute name='name' alias='opportunity_count' aggregate='countcolumn' />  
  <attribute name='ownerid' alias='ownerid' groupby='true' />
  <attribute name='createdon' alias='createdon' /> 
  <attribute name='customerid' alias='customerid' /> 
 </entity> 
</fetch>";

EntityCollection groupby1_result = orgProxy.RetrieveMultiple(new FetchExpression(groupby1));

foreach (var c in groupby1_result.Entities)
{
  Int32 count = (Int32)((AliasedValue)c["opportunity_count"]).Value;
  string OwnerID = ((EntityReference)((AliasedValue)c["ownerid"]).Value).Name;
  DateTime dtCreatedOn = ((DateTime)((AliasedValue)c["createdon"]).Value);
  string CustomerName = ((EntityReference)((AliasedValue)c["customerid"]).Value).Name;
}

But I get this error:

EXCEPTION: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: An attribute can not be requested when an aggregate operation has been specified and its neither groupby nor aggregate. NodeXml : (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).

How do you use aggregates on some values and not others?

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

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

发布评论

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

评论(2

溺孤伤于心 2024-12-13 08:21:38

错误消息为您提供了答案 - FetchXML 不可能做到这一点。您需要进行两次 FetchXML 调用;一种用于您的聚合,另一种用于您的数据。

The error message is giving you the answer - this isn't possible with FetchXML. You'll need to make two FetchXML calls; one for your aggregates and one for your data.

掩耳倾听 2024-12-13 08:21:38

所以,这里发生了一些事情。问题不在于您的查询无法使用 FetchXML 实现,而在于它无法使用任何查询语言(包括 SQL)实现。上面的 FetchXML 的 SQL 如下:

SELECT ownerID, createdon, customerid
FROM dbo.Opportunity
GROUP BY ownerID

如果您尝试对数据库运行此 SQL,您会得到标准的 Column 'dbo.Opportunity.CreatedOn' is invalid in the select list because it is not contains in聚合函数或 GROUP BY 子句。 错误,这正是您的异常试图告诉您的内容。要解决此问题,您还需要通过包含 groupby='true'< 来按每个非聚合列(在本例中为 createdoncustomerid)进行分组每个相应 attribute 元素中的 /code> 属性。

其次,按每个机会的创建日期进行分组或多或少是徒劳的分组,因为机会通常是在不同的日期时间创建的,因此这种分组只会返回整个表。微软正确地认识到了这一点,因此,如果您修复上面的分组问题,您将遇到另一个与日期时间列相关的异常。如果您继续阅读这篇文章我会之前分享过,有一个示例解决了您可以使用不同的日期间隔(年、月、季度等)进行分组的不同方式日期分组 属性。

但是,我感觉即使在解决了一般分组问题和日期分组问题之后,结果集可能仍然不是您想要的。如果不是,发布您希望看到的结果集示例可能会有所帮助,然后解决 FetchXML 是否有能力向您提供该结果集。

So, there are a few things going on here. The problem isn't so much that your query is not achievable with FetchXML as it is it's not achievable in any query language, including SQL. The SQL of your FetchXML above is as follows:

SELECT ownerID, createdon, customerid
FROM dbo.Opportunity
GROUP BY ownerID

If you try to run this SQL against your database, you'd get the standard Column 'dbo.Opportunity.CreatedOn' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. error, which is exactly what your exception is trying to tell you. To solve this, you need to also group by every non-aggregated column (in this case, createdon and customerid) by including the groupby='true' attribute in each respective attribute element.

Secondly, grouping by the create date of each opportunity is more or less a fruitless grouping, as opportunities are generally created on different datetimes, so this grouping would just return the whole table. Microsoft rightly recognizes this, so if you fix the grouping issue above, you will encounter another exception related to the datetime column. If you continue reading the article I'd previously shared, there is an example that address the different ways you can group by different date intervals (year, month, quarter, etc.) using the dategrouping attribute.

But, I get the feeling that even after fixing the general grouping issues, and then the dategrouping issue, the result set might still not be what you want. If it's not, it might help to post an example of a result set that you'd expect to see and then address whether FetchXML has the power to deliver that set to you.

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