4

我想创建一个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 Conditionsand中做到这一点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);
            }
4

2 回答 2

7

希望这应该是不言自明的。

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()
                }
            }
        }
    }
};
于 2016-05-12T07:38:11.527 回答
2

除了 James 的回答,别忘了您还可以使用已有的 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 执行要容易得多。

于 2016-05-12T07:45:21.800 回答