gpt4 book ai didi

c# - 构建名称字段为 A 或 B 的 QueryExpression

转载 作者:太空狗 更新时间:2023-10-29 23:11:21 25 4
gpt4 key购买 nike

我正在尝试构建 Dynamics CRM 4 查询,以便我可以获得名为“事件 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);

如果我能得到一些关于如何获取我需要的数据的建议,我将不胜感激。

最佳答案

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 对象,就像我在上面所做的那样。

关于c# - 构建名称字段为 A 或 B 的 QueryExpression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3051039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com