C#CRM QueryExpression LinkEntity通过多个字段加入

时间:2012-07-11 21:12:59

标签: c# dynamics-crm crm

是否可以在CRM中动态加入多个字段?

我在SQL中的意思是这样的

select field1, field2, ..., fieldN
from ServiceAppointment
inner join bh_product
on bh_product.bh_contract = ServiceAppointment.bh_product.bh_contract
and bh_product.serviceid = ServiceAppointment.serviceid

我正在尝试使用queryexpression来提出上述内容,但我没有得到我想要的所需行为,并且我结束了大量的记录而不是预期。如果我知道之前我知道字段编号2的值,我可以这样做,但在运行时我不知道,查询必须加入2个字段。我的实际代码在下面,减去了我无法想出的部分。

这不起作用......我只期待2条记录,但我得到了数百......

var leContact = new LinkEntity(ServiceAppointment.EntityLogicalName, ActivityParty.EntityLogicalName, "activityid", "activityid", JoinOperator.Inner);
leContact.LinkCriteria = new FilterExpression();
leContact.LinkCriteria.AddCondition("partyid", ConditionOperator.Equal, contactId);
queryExpression.LinkEntities.Add(leContact);
result = GetServiceActivityList(queryExpression);

var leService = new LinkEntity(ServiceAppointment.EntityLogicalName, BrightHorizons.Shared.CRM.Interface.Service.EntityLogicalName, "serviceid", "serviceid", JoinOperator.Inner);
queryExpression.LinkEntities.Add(leService);
result = GetServiceActivityList(queryExpression);

// THIS IS THE PROBLEM    
    var leProduct = new LinkEntity(ServiceAppointment.EntityLogicalName, bh_product.EntityLogicalName, "bh_contract", "bh_contract", JoinOperator.Inner);
    var leProduct2 = new LinkEntity(ServiceAppointment.EntityLogicalName, bh_product.EntityLogicalName, "serviceid", "bh_service", JoinOperator.Inner);
    queryExpression.LinkEntities.Add(leProduct2);
    queryExpression.LinkEntities.Add(leProduct);
    result = GetServiceActivityList(queryExpression);

//THIS ALSO DOESNT WORK 
    var leProduct = new LinkEntity(ServiceAppointment.EntityLogicalName, bh_product.EntityLogicalName, "bh_contract", "bh_contract", JoinOperator.Inner);
    leProduct.AddLink(bh_product.EntityLogicalName, "bh_service", "bh_service", JoinOperator.Inner);
    queryExpression.LinkEntities.Add(leProduct);
    result = GetServiceActivityList(queryExpression);

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我不确定你是否对它进行了调查,但Linq(msdn examples)中的连接非常简单。我写了这样的Contact-Accounts连接:

var appContacts = (
from c in ctx.contacts
join a in ctx.accounts on c.contactid equals a.primarycontactid
where a.name.Contains("Contoso")
select new { c.contactid, c.fullname }).ToList());

a good thread on N:N over here