使用 FetchXML 构建查询 – MS CRM





0/5 (0投票)
使用 FetchXML 构建查询 – MS CRM
引言
MS CRM 提供了不同的查询模式来构建查询。在本模块中,我们将讨论一些关于使用 Fetch XML 构建查询的理论,然后进行演示。
使用 Fetch XML,你可以使用所有标准查询运算符和部分值。Fetch XML 非常强大。你可以使用查询表达式或日期相关的示例进行所有这些检查。你还可以组合多个条件。
- 标准运算符示例
- eq, neq, ne, gt, ge, le, lt, like, not-like, in, not-in, null, not-null
- 部分值示例
- 以...开头, 以...结尾, 不以...结尾, 不以...开头
- 日期相关示例
- 今天, 明天, 过去七天, 本周, 本月, 下个月
- 本财政年度, 下个财政年度
- 在, 在...之前/之后
获取相关数据
- 引用并包含来自 N:1 相关实体的数据很有用
- 我们可以根据相关数据指定筛选条件
使用聚合
Fetch XML 的独特而强大的功能是聚合。
- 在
<fetch
标签上启用聚合,使用aggregate= true
和distinct=false
- 可以有一个或多个具有指定聚合的属性
Avg
,Count
,Max
,Min
和Sum
示例
- 在计算平均值时,不考虑空值,而是考虑零。
- 空值, 100, 100, 1000 的平均值为 100
- 空值, 100, 0, 1000 的平均值为 66.66
- 空值, 100, 空值, 100 的平均值为 100
<fetch distinct='false' mapping ='logical' aggregate='true'>
<entity name='opportunity'>
<attribute name='estimatedvalue' aggregate='avg' alias='estimatedvalue_avg' />
<attribute name='estimatedvalue' aggregate='min' alias='estimatedvalue_min' />
<attribute name='estimatedvalue' aggregate='max' alias='estimatedvalue_max' />
</ entity>
</ fetch>
使用分组
分组是我们可以做的另一个强大的功能。
- 可以按常规属性分组 – 不能按虚拟属性分组
- 对于日期,必须指定 dategrouping。例如:年、季度、月、周或日
- 可以为链接实体指定组
<fetch distinct='false' mapping ='logical' aggregate='true'>
<entity name='opportunity'>
<attribute name='statecode' groupby='true' alias='state' />
<attribute name='createdon' groupby='true' alias='state' dategroupping='month' alias='created' />
<attribute name='estimatedvalue' aggregate='avg' alias='estimatedvalue_avg' />
<attribute name='estimatedvalue' aggregate='min' alias='estimatedvalue_min' />
<attribute name='estimatedvalue' aggregate='max' alias='estimatedvalue_max' />
</ entity>
</ fetch>
Fetch XML 的限制
Fetch XML 查询表达式有一些限制
- 不支持 Union – 相反,你必须使用多个查询
- 没有子查询的概念
- Fetch 聚合默认限制为 50,000 条记录(在线限制为 50,000)
AggregateQueryRecordLimit
是本地部署的设置 - 链接实体限制为
10
个 - 条件不能是实体上的其他字段
- 无法在链接实体之间执行“或”条件
演示
在这里,我们将针对名为 Account
的实体构建一个聚合查询。
- 转到文件 >> 单击新建 >> 单击项目 >> 选择控制台应用程序。
- 检查引用。你可以在 CRM SDK 2013 中找到“CrmServiceHelpers.cs”和“DeviceIdManager.cs”文件,或者在下方找到它们
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query; namespace FetchXML_Aggregate { class Program { static void Main(string[] args) { // Obtain the target organization's web address and client logon Microsoft.Crm.Sdk.Samples.ServerConnection serverConnect = new Microsoft.Crm.Sdk.Samples.ServerConnection(); Microsoft.Crm.Sdk.Samples.ServerConnection.Configuration config = serverConnect.GetServerConfiguration(); OrganizationServiceProxy _serviceProxy; using (_serviceProxy = Microsoft.Crm.Sdk.Samples.ServerConnection.GetOrganizationProxy(config)) { string strAggregate = @"<fetch mapping='logical' aggregate='true' distinct='false' > <entity name='account'> <attribute name='creditlimit' alias='creditLimitAvg' aggregate='avg' /> </entity> </fetch>"; var aggResults = _serviceProxy.RetrieveMultiple(new FetchExpression(strAggregate)); foreach (var c in aggResults.Entities) { decimal creditLimitAvg = ((Money)c.GetAttributeValue <AliasedValue>("creditLimitAvg").Value).Value; Console.WriteLine("Total : " + decimal.Floor(creditLimitAvg)); Console.Read(); } } } } }
- 按 F5。
你可以获得
creditlimit
的平均值。