在 CRM 动态中使用 select 计数 SDK.Query.QueryExpression

Use select count in CRM dynamics SDK.Query.QueryExpression

使用 c#,我需要使用 Microsoft Dynamics 2015 的 SDK.Query.QueryExpression 库构建查询。我无法弄清楚如何排序和计算出现次数。

我想统计每个产品在所有机会中的使用次数。

sql 查询本身相当简单:

SELECT b.Name, count(a.ProductId) as 'accurances'
FROM [ProkonCRM_MSCRM].[dbo].[OpportunityProductBase] a,
     [ProkonCRM_MSCRM].[dbo].[ProductBase] b
where a.ProductId = b.ProductId
group by b.name 

据我所知,QueryExpressions 不支持聚合函数。 您可能需要为此查看 FetchXML,因为那里支持聚合函数。 以下是一些使用 FetchXML 实现您想要的示例: https://msdn.microsoft.com/en-us/library/gg309565.aspx#count

FetchXML 中的示例(未经测试,您的实体名称可能不同,我假设这是一个 N:N 关系)

<fetch distinct='false' mapping='logical' aggregate='true'> 
    <entity name='product'> 
       <attribute name='name' alias='productName' groupby='true'/> 
       <link-entity name='opportunityproduct' from='opportunityid' to='opportunityid'>
           <attribute name='productid' alias='occurences' aggregate='count' />
       </link-entity> 
    </entity> 
</fetch>

我假设您有 CRM 2015 的本地实施。 基于该假设,您可以选择直接查询 CRM 数据库并使用 C# 执行 SQL 查询(例如使用 Microsoft.Data.SQL 库)。

查询 FilteredViews 很重要,因为这是直接查询 CRM 数据库的推荐方法。

FilteredViews 参考:https://technet.microsoft.com/en-us/library/dn531182.aspx