2010-06-16 62 views
5

我正在嘗試構建一個Dynamics CRM 4查詢,以便可以獲取名爲「事件A」或「事件B」的日曆事件。構建一個QueryExpression,其中名稱字段是A或B

QueryByAttribute似乎沒有做這項工作,因爲我無法指定一個條件,其中「event_name」=「Event A」的字段爲「event_name」=「Event B」。

使用QueryExpression時,我發現FilterExpression適用於引用實體。我不知道FilterExpression是否可以在引用實體上使用。下面的示例與我想要實現的內容類似,不過這將返回一個空的結果集,因爲它將查找名爲「my_event_response」的實體中的「name」屬性。它開始看起來像我將需要運行幾個查詢來獲得這個,但是這比我可以一次全部提交它的效率低。

ColumnSet columns = new ColumnSet(); 
columns.Attributes = new string[]{ "event_name", "eventid", "startdate", "city" }; 

ConditionExpression eventname1 = new ConditionExpression(); 
eventname1.AttributeName = "event_name"; 
eventname1.Operator = ConditionOperator.Equal; 
eventname1.Values = new string[] { "Event A" }; 

ConditionExpression eventname2 = new ConditionExpression(); 
eventname2.AttributeName = "event_name"; 
eventname2.Operator = ConditionOperator.Equal; 
eventname2.Values = new string[] { "Event B" }; 

FilterExpression filter = new FilterExpression(); 
filter.FilterOperator = LogicalOperator.Or; 
filter.Conditions = new ConditionExpression[] { eventname1, eventname2 }; 

LinkEntity link = new LinkEntity(); 

link.LinkCriteria = filter; 

link.LinkFromEntityName = "my_event"; 
link.LinkFromAttributeName = "eventid"; 

link.LinkToEntityName = "my_event_response"; 
link.LinkToAttributeName = "eventid"; 

QueryExpression query = new QueryExpression(); 
query.ColumnSet = columns; 
query.EntityName = EntityName.mbs_event.ToString(); 
query.LinkEntities = new LinkEntity[] { link }; 

RetrieveMultipleRequest request = new RetrieveMultipleRequest(); 
request.Query = query; 

return (RetrieveMultipleResponse)crmService.Execute(request); 

我很感謝關於如何獲得我需要的數據的一些建議。

回答

6

QueryExpression對象具有可以設置的Criteria屬性。如果你正在尋找有一個名稱或名稱B「my_event」記載,設置好它是這樣的:

ColumnSet columns = new ColumnSet(); 
columns.Attributes = new string[]{ "event_name", "eventid", "startdate", "city" }; 

ConditionExpression eventname1 = new ConditionExpression(); 
eventname1.AttributeName = "event_name"; 
eventname1.Operator = ConditionOperator.Equal; 
eventname1.Values = new string[] { "Event A" }; 

ConditionExpression eventname2 = new ConditionExpression(); 
eventname2.AttributeName = "event_name"; 
eventname2.Operator = ConditionOperator.Equal; 
eventname2.Values = new string[] { "Event B" }; 

FilterExpression filter = new FilterExpression(); 
filter.FilterOperator = LogicalOperator.Or; 
filter.Conditions = new ConditionExpression[] { eventname1, eventname2 }; 

QueryExpression query = new QueryExpression(); 
query.ColumnSet = columns; 
query.EntityName = EntityName.mbs_event.ToString(); 
query.Criteria = filter; 

RetrieveMultipleRequest request = new RetrieveMultipleRequest(); 
request.Query = query; 

return (RetrieveMultipleResponse)crmService.Execute(request); 

如果你只找有反應的事件,保持LinkEntity部分,但像上面那樣將FilterExpression移到QueryExpression對象上。

相關問題