Dynamics CRM 2011编程系列(49):FetchExpression(一)
今天我们来看看FetchExpression表达式吧,在Dynamics CRM 系统中它可以说是无所不在呀。大家都知道我们查询数据库中的数据会用到SQL语言,把Dynamics CRM 系统比作数据库的话呢,FetchExpresion就是它的SQL啦。
当然啦,FetchExpression没有SQL那么灵活。但是作为一个用XML来描述查询信息的引擎来说也确实挺棒的!我们先来看一个简单的FetchExpression表达式吧:
用Dynamics CRM 2011 高级查找编辑器编辑如下图中的查询:

高级查找编辑器对应的Fetch表达式
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"> <entity name="account"> <attribute name="accountid" /> <attribute name="address1_city" /> <attribute name="address1_addresstypecode" /> <attribute name="name" /> <order attribute="address1_city" descending="false" /> <filter type="and"> <filter type="or"> <condition attribute="name" operator="eq" value="Microsoft" /> <condition attribute="accountnumber" operator="eq" value="123456" /> </filter> <filter type="and"> <condition attribute="address1_line1" operator="eq" value="ShangHai" /> <condition attribute="address1_telephone2" operator="eq" value="135********" /> </filter> </filter> <link-entity name="systemuser" from="systemuserid" to="owninguser" alias="aa"> <attribute name="fullname" /> <filter type="and"> <condition attribute="fullname" operator="eq" value="Jeff" /> </filter> </link-entity> </entity></fetch>
Fetch表达式并不复杂,很容易通过节点的命名猜出它们的用途。Fetch主要的节点及其描述如下表所示:
在Dynamics CRM 2011 SDK中提供了FetchExpression的Schema文件,如果我们需要制作复杂的Fetch表达式的话,建议引用该Schema文件。
我们来看几个有意思的FetchExpression例子吧:
1. 查询Account实体下存在多少行记录
FetchExpression
<fetch aggregate="true"> <entity name="account"> <attribute name="name" alias="record_count" aggregate="count"/> </entity></fetch>
Result
<resultset morerecords="0"><result><record_count formattedvalue="7">7</record_count></result></resultset>
2. 查询Account实体下字段Telephone1包含值的记录总数
FetchExpression
<fetch aggregate="true"> <entity name="account"> <attribute name="telephone1" alias="column_count" aggregate="countcolumn"/> </entity></fetch>
Result
<resultset morerecords="0"><result><column_count formattedvalue="3">3</column_count></result></resultset>
3. 查询Account实体及与其关联的Contact实体,且对account实体进行升序排序,对contact实体进行降序排序
<fetch aggregate="false">BeyondSoft Hitesh Wicresoft Jim Wicresoft Hitesh Wicresoft Clark Wicresoft Bob
Result
<resultset morerecords="0"><result><name>BeyondSoft</name><c.firstname>Hitesh</c.firstname></result><result><name>Wicresoft</name><c.firstname>Jim</c.firstname></result><result><name>Wicresoft</name><c.firstname>Hitesh</c.firstname></result><result><name>Wicresoft</name><c.firstname>Clark</c.firstname></result><result><name>Wicresoft</name><c.firstname>Bob</c.firstname></result></resultset>
4. 查询公司名称以Wic开头,且客户的首字母为J开头的记录数
<fetch aggregate="true"> <entity name="account"> <attribute name="name" aggregate="count" alias="c"/> <link-entity from="contactid" to="primarycontactid" name="contact" alias="c"> <filter type="and"> <condition attribute="fullname" operator="begins-with" value="j"></condition> </filter> </link-entity> <filter type="and"> <condition attribute="name" operator="begins-with" value="wic"></condition> </filter> </entity></fetch>
Result
<resultset morerecords="0"><result><c formattedvalue="1">1</c></result></resultset>