我想创建一个QueryExpression
来模拟这个SQL语句
select * from A
inner join B on A.b_id=B.ID
where B.Name like "% what ever %"
这就是FetchXML的样子
<?xml version="1.0" encoding="UTF-8"?>
<fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0">
<entity name="A">
<attribute name="ID" />
<attribute name="sce_name" />
<link-entity name="B" alias="ab" to="b_id" from="A">
<filter type="and">
<condition attribute="Name" value="% what ever %" operator="like" />
</filter>
</link-entity>
</entity>
</fetch>
我如何在QueryExpression
LinkQuery
Conditions
和Filters
中完成此操作,我也不想从 B 开始因为 A 也可能有其条件。
这是我到目前为止所尝试的
QueryExpression query = new QueryExpression("A");
query.ColumnSet.AllColumns = true;
var link = new LinkEntity()
{
JoinOperator = JoinOperator.Inner,
EntityAlias = "c",
LinkFromEntityName = "A",
LinkToEntityName = "B",
LinkFromAttributeName = "b_id",
LinkToAttributeName = "ID",
};
using (var Service = new OrganizationService("con"))
{
EntityCollection entities = Service.RetrieveMultiple(query);
}
答案 0 :(得分:5)
希望这应该是自我解释的。
QueryExpression query = new QueryExpression("a") //Start on A
{
ColumnSet = new ColumnSet(), //Columns to retrieve from A
Criteria = new FilterExpression(LogicalOperator.And) //Conditions for A
{
Conditions =
{
new ConditionExpression()
}
},
LinkEntities =
{
//Link to B
new LinkEntity("a", "b", "aid", "bid", JoinOperator.Inner)
{
Columns = new ColumnSet(), //Columns to retrieve from B
LinkCriteria = new FilterExpression() //Conditions for B
{
Conditions =
{
new ConditionExpression()
}
}
}
}
};
答案 1 :(得分:2)
除了詹姆斯的回答,不要忘记你也可以使用你已经拥有的fetch语句进行查询:
RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest
{
Query = new FetchExpression(
@"<fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0">
<entity name="A">
...
</entity>
</fetch>");
};
我很少写出QueryExpressions,因为使用fetch执行要容易得多。