构建一个QueryExpression,其中name字段是A或B.

时间:2010-06-16 06:03:37

标签: c# dynamics-crm dynamics-crm-4

我正在尝试构建Dynamics CRM 4查询,以便我可以获取名为“事件A”或“事件B”的日历事件。

QueryByAttribute似乎没有完成这项工作,因为我无法指定“event_name”=“事件B”的“event_name”=“Event A”字段的条件。

使用QueryExpression时,我发现FilterExpression适用于引用实体。我不知道FilterExpression是否可以在Referenced Entity上使用。下面的示例类似于我想要实现的内容,尽管这将返回一个空结果集,因为它将在名为“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);

我很欣赏有关如何获取所需数据的一些建议。

1 个答案:

答案 0 :(得分:6)

QueryExpression对象具有您可以设置的Criteria属性。如果您要查找名称为A或名称为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对象,就像我上面一样。