我有一个查询应该返回“A”实体,同时链接到实体“B”和“C”,这两个链接有“左外连接”操作符,这样我就可以从一个链接接收“A”实体或其他。
执行后,它只返回一条记录,这对于两个链接标准是常见的。如果我删除实体“B”的链接,那么我会得到符合链接条件的预期记录。当我删除实体“C”的链接时,同时发生了与实体“B”的链接。
所以,我假设我的查询仅适用于一个链接,但是当有两个链接并且我不明白为什么时,它不能按预期工作。
实际上我正在关注MSDN example。
这是我的数据:
_____________
| A |
-------------
| a_id |
| 1 | - entity with this id is the only record in the results
| 2 | - entity with this id should appear in the results, but it is not
| 3 | - entity with this id should appear in the results, but it is not
_________________________________
| B |
---------------------------------
| b_one_id | b_two_id |
| 1 | 1 | - matches link criteria with alias "connectionB"
| 2 | 1 | - matches link criteria with alias "connectionB"
| 1 | 444 | - doesn't match link criteria, should not appear
_________________________________
| C |
---------------------------------
| c_one_id | c_two_id |
| 1 | 2 | - matches link criteria with alias "connectionC"
| 3 | 2 | - matches link criteria with alias "connectionC"
| 1 | 555 | - doesn't match link criteria, should not appear
我的查询表达式:
QueryExpression query = new QueryExpression
{
EntityName = A.EntityLogicalName,
ColumnSet = new ColumnSet
{
AllColumns = false,
Columns =
{
"a_id",
"a_name"
}
}
};
query.Distinct = true;
query.AddLink(B.EntityLogicalName, "a_id", "b_one_id", JoinOperator.LeftOuter);
query.LinkEntities[0].EntityAlias = "connectionB";
query.LinkEntities[0].LinkCriteria.AddCondition("b_two_id", ConditionOperator.Equal, 1);
query.AddLink(C.EntityLogicalName, "a_id", "c_one_id", JoinOperator.LeftOuter);
query.LinkEntities[0].EntityAlias = "connectionC";
query.LinkEntities[0].LinkCriteria.AddCondition("c_two_id", ConditionOperator.Equal, 2);
// doesn't work as expected
query.Criteria.AddFilter(LogicalOperator.Or);
query.Criteria.Filters[0].AddCondition("connectionB", "b_one_id", ConditionOperator.NotNull);
query.Criteria.Filters[0].AddCondition("connectionC", "c_one_id", ConditionOperator.NotNull);
// doesn't work as expected either
query.Criteria.AddCondition("a_id", ConditionOperator.NotNull);
查询表达式转换为以下Fetch XML查询:
<fetch distinct="true" no-lock="false" mapping="logical">
<entity name="A">
<attribute name="a_id" />
<attribute name="a_name" />
<filter type="and">
<filter type="or">
<!-- doesn't work as expected -->
<condition attribute="b_one_id" operator="not-null" entityname="connectionB" />
<condition attribute="c_one_id" operator="not-null" entityname="connectionC" />
<!-- doesn't work as expected either -->
<condition attribute="a_id" operator="not-null" entityname="A" />
</filter>
</filter>
<link-entity name="B" to="a_id" from="b_one_id" link-type="outer" alias="connectionB">
<filter type="and">
<condition attribute="b_two_id" operator="eq" value="1" />
</filter>
</link-entity>
<link-entity name="C" to="a_id" from="c_one_id" link-type="outer" alias="connectionC">
<filter type="and">
<condition attribute="c_two_id" operator="eq" value="2" />
</filter>
</link-entity>
</entity>
</fetch>
答案 0 :(得分:0)
我希望您的代码中存在一些编程错误。 查看您的代码部分
query.AddLink(B.EntityLogicalName, "a_id", "b_one_id", JoinOperator.LeftOuter);
query.LinkEntities[0].EntityAlias = "connectionB";
query.LinkEntities[0].LinkCriteria.AddCondition("b_two_id", ConditionOperator.Equal, 1);
query.AddLink(C.EntityLogicalName, "a_id", "c_one_id", JoinOperator.LeftOuter);
query.LinkEntities[0].EntityAlias = "connectionC";
query.LinkEntities[0].LinkCriteria.AddCondition("c_two_id", ConditionOperator.Equal, 2);
您正在添加query.AddLink()并再次添加一些其他链接以进行查询 但是为Alias和LinkCriteria使用相同的链接即query.LinkEntities [0]。 可能是纠正这个错误可以解决你的问题。