如何向鏈接實體添加過濾條件(例如,在此情況下爲電子郵件)? 將過濾條件添加到給我重複行的鏈接條件。Dynamics CRM 2011 LinkEntities左連接問題
等效的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();
我正在尋找所有不在電子郵件實體中的機會。我使用左連接在sql和queryexpression類中都得到了這個問題。問題是我無法在電子郵件實體上添加過濾器。我改變了代碼以反映filter2中的關於objectid。 – mdvr
@Madhavi是爲特定的電子郵件或所有電子郵件?如果它是一個特定的電子郵件,那麼在你的左連接中指定電子郵件,並且你的SQL示例的最終語句應該是「and ** o **。RegardingObjectId爲null」 –
SQL對於需求正常工作。我無法在控制檯應用程序中的查詢表達式中輸入critieria(e.regardingobject爲null)。 – mdvr