用于查询表达式的SQL语句

时间:2017-11-15 08:04:21

标签: c# sql sql-server dynamics-crm query-expressions

我想将SQL语句转换为Query Expression。

   SELECT q.EMailAddress, fe.torecipients, fe.subject, fe.createdon, fe.actualend ,fe.ActivityId
    FROM QueueItem qi WITH (NOLOCK)
    inner join Email fe WITH (NOLOCK) ON fe.ActivityId = qi.ObjectId and fe.RegardingObjectId is null
    inner join Queue q WITH (NOLOCK) ON q.QueueId= qi.QueueId and q.EMailAddress is not null
    WHERE PATINDEX(CONCAT('%', q.emailaddress, '%'), fe.torecipients ) >= 1 
    ORDER BY fe.createdon DESC

我能做到这一点。如何转换' PATINDEX(CONCAT('%',q.emailaddress,'%'),fe.torecipients)> = 1'。你能救我吗?

QueryExpression queueQuery = new QueryExpression(Constants.QueryQueueItem);
queueQuery.ColumnSet.AddColumns(Constants.QueryIncidentId);

LinkEntity emailLink = new LinkEntity();
emailLink.LinkFromEntityName = Constants.QueryQueueItem;
emailLink.LinkToAttributeName = Constants.QueryEmail;
emailLink.LinkFromAttributeName = "ObjectId";
emailLink.LinkToAttributeName = "ActivityId";
emailLink.JoinOperator = JoinOperator.Inner;
emailLink.LinkCriteria.AddCondition(new ConditionExpression("RegardingObjectId", ConditionOperator.Null, true))

LinkEntity queueLink = new LinkEntity();
queueLink.LinkFromEntityName = "QueueItem";
queueLink.LinkToAttributeName = "Queue";
queueLink.LinkFromAttributeName = "QueueId";
queueLink.LinkToAttributeName = "QueueId";
queueLink.JoinOperator = JoinOperator.Inner;
queueLink.LinkCriteria.AddCondition(new ConditionExpression("EMailAddress", ConditionOperator.NotNull, true));

queueQuery.LinkEntities.Add(emailLink);
queueQuery.LinkEntities.Add(queueLink);

ConditionExpression emailCondition = new ConditionExpression();
emailCondition.AttributeName = Constants.QueryIncidentId;
emailCondition.Operator = ConditionOperator.NotNull;

FilterExpression emailFilter = new FilterExpression();
emailFilter.Conditions.Add(emailCondition);
queueQuery.Criteria = emailFilter;

1 个答案:

答案 0 :(得分:0)

FetchXML不支持PATINDEX函数,因此快速选择使用Like运算符:

new ConditionExpression(“torecipients”, ConditionOperator.Like, "%"+q.emailaddress+"%");

允许您匹配电子邮件地址的更复杂选项是加入电子邮件的活动方并根据活动方类型和AddressUsed进行过滤。