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 keyword。要创建的聚合的属性。
         * 请设置keywordaggregate到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 = ((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 = ((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 = (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 = (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 = ((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 = ((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 = (Int32)((AliasedValue)en[“new_value_count”]).Value;
                decimal max_value = ((Money)((AliasedValue)en[“new_value_max”]).Value).Value;
                decimal min_value = ((Money)((AliasedValue)en[“new_value_min”]).Value).Value;
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116226.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • microbiomeViz:绘制lefse结果中Cladogram「建议收藏」

    microbiomeViz:绘制lefse结果中Cladogram「建议收藏」平日经常会分析shotgun宏基因组的数据,我们的pipeline使用MetaPhlAn,Kraken等profiler。这种数据经常会产生一个表格,如下download.file(“https://bitbucket.org/biobakery/biobakery/raw/tip/demos/biobakery_demos/data/metaphlan2/output/SRS014459-Stool_profile.txt”,’SRS014459-Stool_profile.txt’)knitr

    2022年5月18日
    42
  • recvfrom设置超时

    recvfrom设置超时structtimevaltv;intret;tv.tv_sec=10;tv.tv_usec=0;if(setsockopt(s,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv))<0){ printf("socketoptionSO_RCVTIMEOnotsupport\n"); return;}if((ret

    2022年7月23日
    15
  • 微信H5页面 会被软键盘顶起来

    微信H5页面 会被软键盘顶起来

    2021年7月5日
    76
  • waitforsingleobject的作用_效率理论

    waitforsingleobject的作用_效率理论MicrosoftWindows平台中两种最常用的锁定方法为WaitForSingleObject和EnterCriticalSection。WaitForSingleObject是一个过载MicrosoftAPI,可用于检查和修改许多不同对象(如事件、作业、互斥体、进程、信号、线程或计时器)的状态。WaitForSingleObject的一个不足之处是它会始终获取内核的锁

    2022年9月19日
    3
  • Nginx负载均衡算法有哪些?_netty负载均衡

    Nginx负载均衡算法有哪些?_netty负载均衡1.轮询(roundrobin)(默认)轮询方式,依次将请求分配到各个后台服务器中,默认的负载均衡方式。适用于后台机器性能一致的情况。挂掉的机器可以自动从服务列表中剔除。2.加权(weight)根据权重来分发请求到不同的机器中,指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。例如:upstreambakend{serv…

    2022年10月12日
    2
  • 《JavaScript 模式》读书笔记(3)— 字面量和构造函数3

    这是字面量和构造函数的最后一篇内容,其中包括了JSON、正则表达式字面量,基本值类型包装器等知识点。也是十分重要的哦。五、JSONJSON是指JavaScript对象表示以及数据传输格式。它是一种

    2022年3月25日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号