Microsoft Dynamics CRM 2011的组织服务中的RetrieveMultiple方法(转)
本篇文章,介绍Microsoft Dynamics CRM 2011的组织服务中的RetrieveMultiple方法。
RetreiveMultiple方法,用于获取实体的多个实例,该方法的签名如下:
1 public virtual EntityCollection RetrieveMultiple (
2 QueryBase query
3 )
RetreiveMultiple方法的输入参数是QueryBase类的对象。QueryBase是一个基类,有多个子类,其类树如下图所示:
由上图可见,QueryBase有三个派生类,分别是QueryByAttribute、QueryExpression以及FetchExpression。下面依次进行介绍。
QueryByAttribute类。使用QueryByAttribute类生成根据一组值测试一组属性的查询。该类的主要属性列表如下所示:
属性 |
说明 |
EntityName |
指定检索哪种类型的实体。一个查询表达式仅检索一个实体类型集合。 |
ColumnSet |
指定要检索的属性(列)的集合。 |
Attributes |
指定查询中选择的属性集合。 |
Values |
指定查询执行时要查找的属性值。 |
Orders |
指定从查询返回记录的顺序。 |
PageInfo |
指定从查询返回的页数和每页的记录数。 |
借由QueryByAttribute,形成的等价SQL语句如下:

1 Select
2 [ColumnSet]
3 From
4 [EntityName]
5 Where
6 Attibutes[1] = Values[1] And
7 Attibutes[2] = Values[2] And
8 .......
9 Attibutes[N] = Values[N]

其中,所有处于方括号(“[”与“]”)之中的内容,均是来自于QueryByAttribute的各个属性值。样例代码如下:

1 //创建QueryByAttribute实例,指定待检索实体的逻辑名称account
2 QueryByAttribute queryByAttribute = new QueryByAttribute("account");
3
4 //指定返回结果包含的数据列集合
5 queryByAttribute.ColumnSet = new ColumnSet("name", "address1_city", "emailaddress1");
6
7
8 //指定检索条件,address1_city = ‘Detroit’
9
10 //首先指定检索条件的列字段信息
11 queryByAttribute.Attributes.AddRange("address1_city");
12 //接着指定检索条件中的列值信息
13 queryByAttribute.Values.AddRange("Detroit");
14
15 //指定检索条件,accountnumber = ‘abc’
16
17 //首先指定检索条件的列字段信息
18 queryByAttribute.Attributes.AddRange("accountnumber");
19 //接着指定检索条件中的列值信息
20 queryByAttribute.Values.AddRange("abc");
21
22
23 //调用组织服务的RetrieveMultiple方法,将queryByAttribute对象传递给RetrieveMultiple方法
24 EntityCollection retrieved = _serviceProxy.RetrieveMultiple(queryByAttribute);
25 //遍历返回结果
26 foreach (var c in retrieved.Entities)
27 {
28 System.Console.WriteLine("Name: " + c.Attributes["name"]);
29 System.Console.WriteLine("Address: " + c.Attributes["address1_city"]);
30 System.Console.WriteLine("E-mail: " + c.Attributes["emailaddress1"]);
31 }

FetchExpression,最主要的属性就是Query属性,可以将FetchXml字符串赋值给Query属性。FetchXml是MS CRM的专有查询语言,FetchXml包括能够计算总和、平均值、最小值、最大值和计数的分组和聚合函数:sum/avg/min/max/count(*)/count(属性名称)。FetchXml的书写非常之繁复,可以通过第三方提供的工具,或者通过“高级查找”界面中的“下载FetchXml”获得。首先,利用CRM提供的图形化界面创建需要的查找,而后点击Ribbon工具条中的“下载FetchXml”按钮,如下图所示:
对应于上面查找条件的FetchXml语句如下图所示:
将这个FetchXml字符串赋值给FetchExpression的Query属性,而后调用RetrieveMultiple方法,将FetchExpression对象作为参数传递给该方面即可。下面是样例代码:

1 string query = “<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='account'><attribute name='address1_telephone1' /><attribute name='customersizecode' /><attribute name='accountratingcode' /><attribute name='accountnumber' /><attribute name='name' /><attribute name='accountid' /><order attribute='accountnumber' descending='false' /><order attribute='name' descending='false' /><filter type='and'><condition attribute='primarycontactid' operator='not-null' /><condition attribute='statecode' operator='eq' value='0' /></filter><link-entity name='contact' from='contactid' to='primarycontactid' visible='false' link-type='outer' alias='a_410707b195544cd984376608b1802904'><attribute name='gendercode' /><attribute name='telephone1' /><attribute name='fullname' /></link-entity></entity></fetch>”;
2
3 FetchExpression fetchExpression = new FetchExpression(query);
4
5 EntityCollection retrieved = _serviceProxy.RetrieveMultiple(fetchExpression);

QueryExpression。使用QueryExpression类,可以生成复杂查询,可以使用 ConditionExpression、ColumnSet 和 FilterExpression 类为QueryExpression设置查询参数。QueryExpression 类可用于创建复杂查询。QueryByAttribute 类旨在作为简单的搜索方式,用于搜索属性匹配指定值的实体。
QueryExpression类的主要属性如下表所示:
属性 |
说明 |
EntityName |
指定将检索哪种类型的实体。一个查询表达式仅检索一个实体类型集合。 |
ColumnSet |
指定要检索的属性(列)的集合。 |
Criteria |
指定复杂条件和逻辑筛选器表达式,用于筛选查询结果。 |
Distinct |
指定查询的结果是否包含重复记录。 |
LinkEntities |
指定多个实体类型之间的链接。 |
Orders |
指定从查询返回的记录的顺序。 |
PageInfo |
指定从查询返回的页数和每页中的记录数量。 |
非常重要的一个属性是Criteria,该属性是FilterExpression类型。可以使用FilterExpression类生成表示多个条件的查询。例如,可以创建等效于 SQL 语句(如([FirstName] = 'Joe' OR [FirstName] = 'John') AND [City] = 'Redmond'
)的查询表达式。FilterExpression的属性列表如下所示:
属性 |
说明 |
Conditions |
获取或设置包括属性、条件运算符和属性值的条件表达式。 |
FilterOperator |
获取或设置逻辑 AND/OR 筛选器运算符。它使用 LogicalOperator 枚举进行设置。 |
Filters |
获取或设置用于筛选查询结果的条件和逻辑筛选器表达式的层次结构。 |
FilterExpression 类还包含一些帮助程序方法,通过它们可以更轻松地创建查询。AddCondition 方法可将 ConditionExpression 添加到 FilterExpression 的 Conditions 属性中,以减少构造条件表达式所需的代码量。AddFilter 方法可将新筛选器添加到 FilterExpression 类的 Filters 属性中。
除了Criteria之外,还需要注意的是LinkEntities属性,该属性用以指定多个实体之间的链接,从而形成 Account Join Contact On Account.PrimaryContactId = Contact.ContactId Join SystemUser On Account.OwnerId = SystemUser.SystemUserId样式的SQL语句。不过呢,该属性是只读的,需要使用
AddLink ( string linkToEntityName, string linkFromAttributeName, string linkToAttributeName )
或者
AddLink (string linkToEntityName, string linkFromAttributeName, string linkToAttributeName, JoinOperator joinOperator)方法创建实体之间的链接。
使用QueryExpression的样例代码如下所示,:

1 //创建样例数据
2
3 //创建联系人记录
4
5 Entity contact = new Entity("contact");
6 contact.Attributes["firstname"] = "ContactFirstName";
7 contact.Attributes["lastname"] = "ContactLastName";
8 Guid contactId = _orgService.Create(contact, null);
9
10 //创建三个客户记录,指定客户名称分别为“Test Account1”、“Test Account2”、“Test Account3”,并指定三个客户记录的主要联系人属性值为之前创建的联系人记录
11
12 Entity account = new Entity("account");
13 account["name"] = "Test Account1";
14 EntityReference primaryContactId = new EntityReference("contact", contactId);
15 account["primarycontactid"] = primaryContactId;
16
17 Guid accountId1 = _orgService.Create(account, null);
18 account["name"] = "Test Account2";
19 Guid accountId2 = _orgService.Create(account, null);
20 account["name"] = "Test Account3";
21 Guid accountId3 = _orgService.Create(account, null);
22
23 //创建QueryExpression实例,
24 QueryExpression qe = new QueryExpression();
25
26 //指定查询表达式要查询的实体为account
27 qe.EntityName = "account";
28
29 //指定返回值的列信息
30 qe.ColumnSet = new ColumnSet();
31 qe.ColumnSet.Columns.Add("name");
32
33 //指定链接的实体的信息
34
35 qe.LinkEntities.Add(new LinkEntity("account", "contact", "primarycontactid", "contactid", JoinOperator.Inner));
36 qe.LinkEntities[0].Columns.AddColumns("firstname", "lastname");
37 qe.LinkEntities[0].EntityAlias = "primarycontact";
38
39 //调用RetrieveMultiple方法
40
41 EntityCollection ec = _orgService.RetrieveMultiple(qe);
42
43 Console.WriteLine("Retrieved {0} entities", ec.Entities.Count);
44
45 //遍历返回结果
46 foreach (Entity act in ec.Entities)
47 {
48 Console.WriteLine("account name:" + act["name"]);
49 Console.WriteLine("primary contact first name:" + act["primarycontact.firstname"]);
50 Console.WriteLine("primary contact last name:" + act["primarycontact.lastname"]);
51 }
Microsoft Dynamics CRM 2011的组织服务中的RetrieveMultiple方法(转)的更多相关文章
- Microsoft Dynamics CRM 2011 安装完全教程
作者:卞功鑫,转载请保留.http://www.cnblogs.com/BinBinGo/p/4302612.html 环境介绍 WINDOWS 2008 R2 Datacenter Microsof ...
- Microsoft Dynamics CRM 2011 当您在 大型数据集上执行 RetrieveMultiple 查询很慢的解决方法
症状 当您在 Microsoft Dynamics CRM 2011 年大型数据集上执行 RetrieveMultiple 查询时,您会比较慢. 原因 发生此问题是因为大型数据集缓存 Retrieve ...
- Microsoft Dynamics CRM4.0 和 Microsoft Dynamics CRM 2011 JScript 方法对比
CRM 2011 如果需要再IE里面调试,可以按F12在前面加上contentIFrame,比如 contentIFrame.document.getElementById("字段" ...
- How to control PrincipalObjectAccess table growth in Microsoft Dynamics CRM 2011
https://support.microsoft.com/en-us/kb/2664150 How to control PrincipalObjectAccess table growth in ...
- Dynamices CRM JS 类库 神器 XrmServiceToolkit - A Microsoft Dynamics CRM 2011 & CRM 2013 JavaScript Library
XrmServiceToolkit - A Microsoft Dynamics CRM 2011 & CRM 2013 JavaScript Library http://xrmservic ...
- Error message “Assembly must be registered in isolation” when registering Plugins in Microsoft Dynamics CRM 2011 2013 解决办法
Error message “Assembly must be registered in isolation” when registering Plugins in Microsoft Dynam ...
- Microsoft Dynamics CRM 2011 相关-摘自网络
Microsoft Dynamics CRM Server 2011硬件需求: 组件 *最低要求 *推荐配置 处理器 x64 体系结构或兼容的双核 1.5 GHz 处理器 四核 x64 体系结构 2 ...
- 转:JavaScript Reference for Microsoft Dynamics CRM 2011 / 2013
JavaScript Reference for Microsoft Dynamics CRM 2011 / 2013 98 Replies Here’s a quick reference guid ...
- Microsoft Dynamics CRM 2011 面向Internet部署 (IFD) ADFS虚拟机环境搭建的步骤(CRM与ADFS装在同一台服务器上) 摘自网络
1: 安装windows server 2008 R2 中文版 (过程略) 安装完成后设置机器名和IP地址, 本过程机器名 crm5dev,192.168.0.110 dns: 192.168.0.1 ...
随机推荐
- Kerberos和NTLM - SQL Server
当我们使用Windows Authentication去连接SQL Server的时候,SQL Server可能会使用Kerberos或者是NTLM来进行认证,有时间就会因为认证失败的缘故造成各种登录 ...
- 网络编程中常见地址结构与转换(IPv4/IPv6)
1. sockaddr/sockaddr_in/in_addr (IPv4).sockaddr6_in/in6_addr/addrinfo (IPv6) struct sockaddr { unsig ...
- mvc4 web-api 与unity搭建接口
对于接口重要的应该是 功能实现,合法性验证,性能监控,日志等模块 通过unity aop功能可以实现统一的日志模块和性能监控. 1.新建mvc4 webapi项目 nuget添加 unity 3.0+ ...
- delphi 带历史信息的菜单
带历史信息的菜单 实例说明 在有些软件中,菜单栏中可以记录已经打开过的文件信息,使用户操作简单.快捷.当用户要打开已打开过的文件时,不需要重复查找,只需选择菜单中打开过的文件,即可实现打开该文件的操作 ...
- SDWebImage内存性能优化
// 清除已过期图片 [[SDImageCache sharedImageCache] cleanDisk]; // 设置最多缓存7天时间 [[SDImageCache sharedImageCach ...
- Node.js 的Web server--Fenix
Fenix 是提供给开发者使用的简单的一个 Web server, 是基于 Node.js 开发. 能够同一时候在上面执行非常多的项目. 最适合前端开发者使用. 能够通过免费的 Node.js 控制台 ...
- Computer Science Theory for the Information Age-4: 一些机器学习算法的简介
一些机器学习算法的简介 本节开始,介绍<Computer Science Theory for the Information Age>一书中第六章(这里先暂时跳过第三章),主要涉及学习以 ...
- u检验、t检验、F检验、X2检验 (转)
http://blog.renren.com/share/223170925/14708690013 常用显著性检验 1.t检验 适用于计量资料.正态分布.方差具有齐性的两组间小样本比较.包括配对资料 ...
- Logistic回归总结
原文:http://blog.csdn.net/dongtingzhizi/article/details/15962797 Logistic回归总结 作者:洞庭之子 微博:洞庭之子-Bing (2 ...
- Linux系统如何平滑生效NAT-BUGFIX
在< Linux系统如何平滑生效NAT>中,代码有两处问题.这只是目前发现的,没有发现的还有很多很多,这就是我为何不一开始把代码搞复杂的原因. 1.一个bug附带一个优化: 注意以下的代码 ...