微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

crm使用FetchXml分组聚合查询

/* 创建者:菜刀居士的博客
* 创建日期: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; } } } }

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

相关推荐


迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图:提供一种方法顺序访问一个聚合对象中的每个元素,而又不想暴露该对象的内部表示。应用:STL标准库迭代器实现、Java集合类型迭代器等模式结构:心得:迭代器模式的目的是在不获知集合对象内部细节的同时能对集合元素进行遍历操作
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种:(1)同步阻塞IO(BlockingIO):即传统的IO模型。(2)同步非阻塞IO(Non-blockingIO):默认创建的socket都是阻塞的,非阻塞IO要求socket被设置为NONBLOCK。注意这里所说的N
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定义一系列算法,把他们封装起来,并且使他们可以相互替换,使算法可以独立于使用它的客户而变化。应用:排序的比较方法、封装针对类的不同的算法、消除条件判断、寄存器分配算法等。模式结构:心得:对对象(Context)的处理操作可
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个作用于某对象结构中的各元素的操作,它使你在不改变各元素的类的前提下定义作用于这些元素的新操作。应用:作用于编译器语法树的语义分析算法。模式结构:心得:访问者模式是要解决对对象添加新的操作和功能时候,如何尽可能不修改对象的类的一种方
命令模式(Command)命令模式(Command)[Action/Transaction]意图:将一个请求封装为一个对象,从而可用不同的请求对客户参数化。对请求排队或记录请求日志,以及支持可撤消的操作。应用:用户操作日志、撤销恢复操作。模式结构:心得:命令对象的抽象接口(Command)提供的两个
生成器模式(Builder)生成器模式(Builder)意图:将一个对象的构建和它的表示分离,使得同样的构建过程可以创建不同的表示。 应用:编译器词法分析器指导生成抽象语法树、构造迷宫等。模式结构:心得:和工厂模式不同的是,Builder模式需要详细的指导产品的生产。指导者(Director)使用C
设计模式学习心得《设计模式:可复用面向对象软件的基础》一书以更贴近读者思维的角度描述了GOF的23个设计模式。按照书中介绍的每个设计模式的内容,结合网上搜集的资料,我将对设计模式的学习心得总结出来。网络上关于设计模式的资料和文章汗牛充栋,有些文章对设计模式介绍生动形象。但是我相信“一千个读者,一千个
工厂方法模式(Factory Method)工厂方法模式(Factory Method)[Virtual Constructor]意图:定义一个用于创建对象的接口,让子类决定实例化哪一个类,使一个类的实力化延迟到子类。应用:多文档应用管理不同类型的文档。模式结构:心得:面对同一继承体系(Produc
单例模式(Singleton)单例模式(Singleton)意图:保证一个类只有一个实例,并提供一个访问它的全局访问点。应用:Session或者控件的唯一示例等。模式结构:心得:单例模式应该是设计模式中最简单的结构了,它的目的很简单,就是保证自身的实例只有一份。实现这种目的的方式有很多,在Java中
装饰者模式(Decorator)装饰者模式(Decorator)[Wrapper]意图:动态的给一个对象添加一些额外的职责,就增加功能来说,比生成子类更为灵活。应用:给GUI组件添加功能等。模式结构:心得:装饰器(Decorator)和被装饰的对象(ConcreteComponent)拥有统一的接口