/* 创建者:菜刀居士的博客

 * 创建日期: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='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='year' alias='year' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_year = (Int32)((AliasedValue)en["year"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;


            }

        }

/// <summary>

        /// 分组聚合,按季度分组

        /// </summary>

        public void GroupByQuarter(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='quarter' alias='quarter' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_quarter = (Int32)((AliasedValue)en["quarter"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

/// <summary>

        /// 分组聚合,按月分组

        /// </summary>

        public void GroupByMonth(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='month' alias='month' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_month = (Int32)((AliasedValue)en["month"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

/// <summary>

        /// 分组聚合,按周分组

        /// </summary>

        public void GroupByWeek(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='week' alias='week' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_week = (Int32)((AliasedValue)en["week"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

/// <summary>

        /// 分组聚合,按日分组

        /// </summary>

        public void GroupByDay(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='day' alias='day' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_day = (Int32)((AliasedValue)en["day"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

/// <summary>

        /// 分组聚合,多个分组根据

        /// </summary>

        public void GroupByYearAndQuarter(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='year' alias='year' />


                                       <attribute name='actualclosedate' groupby='true' dategrouping='quarter' alias='quarter' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_year = (Int32)((AliasedValue)en["year"]).Value;

                int value_quarter = (Int32)((AliasedValue)en["quarter"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

    }

}

最新文章

  1. 使用SwingBench 对Oracle RAC DB性能 压力测试
  2. IO操作概念。同步、异步、阻塞、非阻塞
  3. ue4 NewObject/StaticConstructObject_Internal/StaticAllocateObject/FObjectInitializer:对象创建和初始化
  4. 似乎都设置了utf-8,为什么出现乱码
  5. openSuSE DNS SERVER CONFIG
  6. UVa 11137 (完全背包方案数) Ingenuous Cubrency
  7. serv-u中如何映射网络驱动器
  8. 安装rlwrap-0.37.tar.gz
  9. springboot自定义starter
  10. 系统编码,文件编码,python编码
  11. SQL Server 2008 R2 链接 Oracle
  12. linux 串口0x03,0x13的问题【转】
  13. 680. Valid Palindrome II
  14. 汇编 REPNE/REPNZ指令,SCASW指令,SCASD指令,SCAS指令
  15. Understanding the STM32F0&#39;s GPIO
  16. EXP 导出出错解决方案
  17. Hadoop2.0构成之HDFS2.0
  18. 完全理解Gson(1):简单入门
  19. UserAgent 设置 php 抓取网页
  20. BER-TLV数据结构

热门文章

  1. hibernate+struts2
  2. C# 处理URL地址
  3. mybatis 简单的入门实例
  4. CAD处理键盘被按下事件(com接口VB语言)
  5. ajax异步请求详解
  6. CentOS6.9下NFS配置说明(转载)
  7. node.js的初级使用
  8. vim使用配置-python
  9. python数字取反~
  10. MySQL之中文乱码问题