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?

有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top