Dynamics CRM 2011 LinkEntities离开了加入问题

时间:2012-06-21 19:47:24

标签: dynamics-crm-2011

如何向链接实体添加过滤条件(例如,在这种情况下为电子邮件)? 为链接条件添加了过滤条件,该条件为我提供了重复的行。

等效的sql查询应如下所示。

select distinct OpportunityId
from Opportunity o 
   left join Email e on e.RegardingObjectId = o.OpportunityId 
   where o.StateCode = 1 and o.StatusCode = 3 
     and e.RegardingObjectId is null

但是QueryExpression类正在执行以下方式。

select distinct opportunityid 
from Opportunity o 
  left join Email e 
    on e.RegardingObjectId = o.OpportunityId 
    and e.RegardingObjectId is null
    where o.StateCode = 1 and o.StatusCode = 3 

代码:

ClientCredentials Credentials = new ClientCredentials();
Credentials.Windows.ClientCredential 
  = System.Net.CredentialCache.DefaultNetworkCredentials;

Uri OrganizationUri = ""
Uri HomeRealmUri = null;
OrganizationServiceProxy orgService 
  = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null);
IOrganizationService _service = (IOrganizationService)orgService;

QueryExpression query = new QueryExpression();
query.Distinct = true;
query.EntityName = "opportunity";
query.ColumnSet = new ColumnSet(true);

FilterExpression filter1 = new FilterExpression();
filter1.FilterOperator = LogicalOperator.And;
filter1.AddCondition("statuscode", ConditionOperator.Equal,3);
filter1.AddCondition("statecode", ConditionOperator.Equal, 1);
query.Criteria = filter1;

LinkEntity linkEntity1 = new LinkEntity();
linkEntity1.JoinOperator = JoinOperator.LeftOuter;
linkEntity1.LinkFromEntityName = "opportunity";
LinkEntity1.LinkFromAttributeName = "opportunityid";
linkEntity1.LinkToEntityName = "email";
linkEntity1.LinkToAttributeName = "regardingobjectid";

query.LinkEntities.Add(linkEntity1);

FilterExpression filter2 = new FilterExpression();

问题出现在这种情况下。我可以在LinkCriteria上使用过滤器,但不能在查询上使用过滤器,因为它是链接实体。

filter2.AddCondition("regardingobjectid", ConditionOperator.Null);
query.LinkEntities[0].LinkCriteria = filter2;

EntityCollection result = _service.RetrieveMultiple(query);

Console.WriteLine(result.Entities.Count());
Console.ReadKey();

2 个答案:

答案 0 :(得分:0)

我不确定发布的查询是否是您要找的...

如果是,那么您应该能够删除过滤器2并添加到过滤器1

filter1.AddCondition("opportunityid", ConditionOperator.Null);

但是将ConcerObjectId与NULL和带有AND操作的OpportunityID进行比较不应该是真的。

答案 1 :(得分:0)

我在向链接实体添加条件方面遇到了类似的问题。我发现我可以使用动态2013 sdk来做到这一点,但你必须看看你是否可以使用2013年sdk来对抗2011年的动态。请参阅Microsoft Dynamics Crm Sdk - Is this query possible?

与2013 SDK的基本区别在于您可以向过滤器添加条件,但为其提供一个实体名称,该名称适用于链接实体。这意味着您实际上并未将条件添加到链接实体本身。

我还在该链接中显示了如何使用Linq提供程序编写查询,这是您可能想要尝试的另一种选择。