使用带IN运算符的嵌套FetchXML获取未归属解决方案的模型驱动应用
问题分析与解决方案
错误原因
你之前的写法错误在于:FetchXML的not-in运算符不支持直接嵌套子查询(即<fetch>标签作为条件值),它仅能接受明确的<value>值列表。系统会把你嵌套的XML内容当成Guid字符串解析,因此抛出“Guid格式不识别”的错误。
正确实现方式
要实现你SQL中的嵌套查询逻辑,应该使用FetchXML支持的not-exists运算符,它对应SQL中的NOT EXISTS子句,能完美实现“排除存在特定条件记录”的需求。
以下是与你SQL逻辑完全匹配的FetchXML:
<fetch distinct="true"> <entity name="solutioncomponent"> <attribute name="objectid" /> <link-entity name="appmodule" from="appmoduleid" to="objectid" link-type="inner" alias="app"> <attribute name="appmoduleid" /> <attribute name="name" /> <!-- 可选,获取应用名称方便识别 --> </link-entity> <filter> <condition attribute="componenttype" operator="eq" value="80" /> </filter> <!-- 核心逻辑:排除存在其他解决方案关联的应用 --> <filter type="not-exists"> <condition attribute="objectid"> <fetch> <entity name="solutioncomponent"> <filter> <condition attribute="objectid" operator="eq" value="@solutioncomponent.objectid" /> <condition attribute="componenttype" operator="eq" value="80" /> <condition attribute="solutionid" operator="not-eq" value="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" /> <condition attribute="solutionid" operator="not-eq" value="yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" /> </filter> </entity> </fetch> </condition> </filter> </entity> </fetch>
补充:如果你需要的是「完全未归属任何解决方案」的应用
如果你的真实需求是获取没有关联任何解决方案的模型驱动应用(而非仅排除特定两个方案),可以用左连接+空值过滤的写法:
<fetch distinct="true"> <entity name="appmodule"> <attribute name="appmoduleid" /> <attribute name="name" /> <link-entity name="solutioncomponent" from="objectid" to="appmoduleid" link-type="outer" alias="sc"> <filter> <condition attribute="componenttype" operator="eq" value="80" /> </filter> </link-entity> <filter> <condition attribute="solutioncomponentid" operator="null" entityname="sc" /> </filter> </entity> </fetch>
内容的提问来源于stack exchange,提问作者redleo85
相关产品推荐
相关产品推荐

