/* 创建者:菜刀居士的博客
* 创建日期:2014年07月09号
*/
namespace Net.CRM.FetchXml
{
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
/// <summary>
/// 使用FetchXml聚合查询,分组依据
/// </summary>
public class FetchXmlExtension
{
/// <summary>
/// 分组聚合
/// sql: select count(*),ownerid from account group by ownerid
/// </summary>
public void Group(IOrganizationService service)
{
string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='name' alias='name_count' aggregate='count' />
<attribute name='ownerid' alias='ownerid' groupby='true' />
</entity>
</fetch>";
EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
if (ec != null && ec.Entities.Count > 0)
{
Entity en = ec.Entities[0];
//获取结果
decimal value = ((Money)((AliasedValue)en["name_count"]).Value).Value;
EntityReference ownerEr = (EntityReference)((AliasedValue)en["ownerid"]).Value;
}
}
/// <summary>
/// 分组聚合,按年分组
/// </summary>
public void GroupByYear(IOrganizationService service)
{
string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name