首页 > 代码库 > crm使用FetchXml聚合查询

crm使用FetchXml聚合查询

/* 创建者:菜刀居士的博客
 * 创建日期:2014年07月08号
 */

namespace Net.CRM.FetchXml
{
    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;

    /// <summary>
    /// 使用FetchXml聚合查询
    /// </summary>
    public class FetchXmlDemo
    {
        /* 特别提示:FetchXML 包括使您能够计算总和、平均值、最小值、最大值和计数的分组和聚合函数。
         * 在查询中只能指定一个 aggregate 属性,而且不能使用 distinct 关键字。要创建的聚合的属性,
         * 请设置关键字aggregate到true,然后指定有效的实体名称, 属性名称,和别名 (变量名)。
         * 同时必须指定要执行的聚合的类型。 
         */


        /// <summary>
        /// 总和
        /// sql: select sum(new_value) as ‘new_value_sum‘ from account
        /// </summary>
        public void Sum(IOrganizationService service)
        {
            string fetchXml = @"<fetch distinct=‘false‘ mapping=‘logical‘ aggregate=‘true‘>
                                    <entity name=‘account‘>
                                        <attribute name=‘new_value‘ alias=‘new_value_sum‘ aggregate=‘sum‘ />
                                    </entity>
                                </fetch>";
            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
            if (ec != null && ec.Entities.Count > 0)
            {
               Entity en = ec.Entities[0];
               //获取结果
               decimal value = http://www.mamicode.com/((Money)((AliasedValue)en["new_value_sum"]).Value).Value;
            }
        }

        /// <summary>
        /// 平均值
        /// sql: select avg(new_value) as ‘new_value_avg‘ from account
        /// 当crm计算数据的平均值时,不考虑 Null 值。但是,会使用零 (0)。
        /// </summary>
        public void Avg(IOrganizationService service)
        {
            string fetchXml = @"<fetch distinct=‘false‘ mapping=‘logical‘ aggregate=‘true‘>
                                    <entity name=‘account‘>
                                        <attribute name=‘new_value‘ alias=‘new_value_avg‘ aggregate=‘avg‘ />
                                    </entity>
                                </fetch>";
            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
            if (ec != null && ec.Entities.Count > 0)
            {
                Entity en = ec.Entities[0];
                //获取结果
                decimal value = http://www.mamicode.com/((Money)((AliasedValue)en["new_value_avg"]).Value).Value;
            }
        }

        /// <summary>
        /// 计算有多少个记录
        /// sql: select count(*) from account
        /// </summary>
        public void Count(IOrganizationService service)
        {
            string fetchXml = @"<fetch distinct=‘false‘ mapping=‘logical‘ aggregate=‘true‘>
                                    <entity name=‘account‘>
                                        <attribute name=‘new_name‘ alias=‘new_name_count‘ aggregate=‘count‘ />
                                    </entity>
                                </fetch>";
            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
            if (ec != null && ec.Entities.Count > 0)
            {
                Entity en = ec.Entities[0];
                //获取结果
                int value = http://www.mamicode.com/(Int32)((AliasedValue)en["new_name_count"]).Value;
            }
        }

        /// <summary>
        /// 计算有多少个记录(针对指定的列名)
        /// sql: select count(distinct new_name) from account
        /// </summary>
        public void CountColumn(IOrganizationService service)
        {
            string fetchXml = @"<fetch distinct=‘false‘ mapping=‘logical‘ aggregate=‘true‘>
                                    <entity name=‘account‘>
                                        <attribute name=‘new_name‘ alias=‘new_name_count‘ aggregate=‘countcolumn‘ distinct=‘true‘ />
                                    </entity>
                                </fetch>";
            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
            if (ec != null && ec.Entities.Count > 0)
            {
                Entity en = ec.Entities[0];
                //获取结果
                int value = http://www.mamicode.com/(Int32)((AliasedValue)en["new_name_count"]).Value;
            }
        }

        /// <summary>
        /// 最大值
        /// sql: select max(new_value) as ‘new_value_max‘ from account
        /// 当crm计算数据的最大值时,不考虑 Null 值。但是,会使用零 (0)。
        /// </summary>
        public void Max(IOrganizationService service)
        {
            string fetchXml = @"<fetch distinct=‘false‘ mapping=‘logical‘ aggregate=‘true‘>
                                    <entity name=‘account‘>
                                        <attribute name=‘new_value‘ alias=‘new_value_max‘ aggregate=‘max‘ />
                                    </entity>
                                </fetch>";
            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
            if (ec != null && ec.Entities.Count > 0)
            {
                Entity en = ec.Entities[0];
                //获取结果
                decimal value = http://www.mamicode.com/((Money)((AliasedValue)en["new_value_max"]).Value).Value;
            }
        }

        /// <summary>
        /// 最小值
        /// sql: select min(new_value) as ‘new_value_min‘ from account
        /// 当crm计算数据的最小值时,不考虑 Null 值。但是,会使用零 (0)。
        /// </summary>
        public void Min(IOrganizationService service)
        {
            string fetchXml = @"<fetch distinct=‘false‘ mapping=‘logical‘ aggregate=‘true‘>
                                    <entity name=‘account‘>
                                        <attribute name=‘new_value‘ alias=‘new_value_min‘ aggregate=‘min‘ />
                                    </entity>
                                </fetch>";
            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
            if (ec != null && ec.Entities.Count > 0)
            {
                Entity en = ec.Entities[0];
                //获取结果
                decimal value = http://www.mamicode.com/((Money)((AliasedValue)en["new_value_min"]).Value).Value;
            }
        }

        /// <summary>
        /// 多个聚合
        /// sql: select count(*) as ‘new_value_count‘,max(new_value) as ‘new_value_max‘,
        ///       min(new_value) as ‘new_value_min‘ from account
        /// </summary>
        public void CountAndMaxAndMin(IOrganizationService service)
        {
            string fetchXml = @"<fetch distinct=‘false‘ mapping=‘logical‘ aggregate=‘true‘>
                                    <entity name=‘account‘>
                                        <attribute name=‘new_value‘ alias=‘new_value_count‘ aggregate=‘count‘ />
                                        <attribute name=‘new_value‘ alias=‘new_value_max‘ aggregate=‘max‘ />
                                        <attribute name=‘new_value‘ alias=‘new_value_min‘ aggregate=‘min‘ />
                                    </entity>
                                </fetch>";
            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
            if (ec != null && ec.Entities.Count > 0)
            {
                Entity en = ec.Entities[0];
                //获取结果
                int count_value = http://www.mamicode.com/(Int32)((AliasedValue)en["new_value_count"]).Value;
                decimal max_value = http://www.mamicode.com/((Money)((AliasedValue)en["new_value_max"]).Value).Value;
                decimal min_value = http://www.mamicode.com/((Money)((AliasedValue)en["new_value_min"]).Value).Value;
            }
        }
    }
}