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

时间:2015-09-03 12:07:47

标签: c# sql dynamics-crm

使用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 

2 个答案:

答案 0 :(得分:2)

据我所知,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>

答案 1 :(得分:0)

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

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

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