2015-12-02 53 views
0

我想在CRM中使用QueryExpression和LinkEntity执行以下SQL等效查询。有任何想法吗?我很难过。我之前使用过LinkEntity,但不确定如何引用或别名相同的实体类型。CRM QueryExpression链接到同一表/实体

select t1.UoMId, t1.BaseUoM, t1.UoMScheduleId, t1.Name, t1.Quantity 
from UoMBase t1 
inner join UoMBase t2 on t1.UoMScheduleId = t2.UoMScheduleId 
where t2.UoMId = '57E59AB7-AC8F-E511-80F0-005056BE36DF'; 

这将导致以下实体记录:

enter image description here

回答

3

尝试以下操作QueryExpression转换:

<fetch mapping="logical" version="1.0"> 
    <entity name="UoM"> 
    <attribute name="UoMId" /> 
    <attribute name="BaseUoM" /> 
    <attribute name="UoMScheduleId" /> 
    <attribute name="Name" /> 
    <attribute name="Quantity" /> 
    <link-entity name="UoM" from="UoMScheduleId" to="UoMScheduleId" alias="t2" link-type="inner"> 
    <filter> 
     <condition attribute="UoMId" operator="eq" value="57E59AB7-AC8F-E511-80F0-005056BE36DF" /> 
    </filter> 
    </link-entity> 
    </entity> 
</fetch> 

QueryExpression query = new QueryExpression("uom"); 
query.ColumnSet = new ColumnSet(new[] { "uomid", "baseuom", "uomscheduleid", "name", "quantity", }); 
query.LinkEntities.Add(new LinkEntity("uom", "uom", "uomscheduleid", "uomscheduleid", JoinOperator.Inner) { EntityAlias = "t2" }); 
query.Criteria.AddCondition("t2", "uomid", ConditionOperator.Equal, guidID); 
EntityCollection result = OrganizationService.RetrieveMultiple(query); 
+0

增加最终代码到你的答案和它的作品。我想知道我的QueryExpression和你的提取是否可以通过更好的别名进行改进。我想知道这一切是否仅仅基于属性和参数的顺序而不起作用,但是非常感谢指针。 – andleer

相关问题