0

是否可以在 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);

我究竟做错了什么?

4

1 回答 1

0

我不确定你是否看过它,但在 Linq 中连接真的很容易(msdn 示例)。我写过这样的 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());

N:N 上有一个很好的线程在这里

于 2012-07-12T15:09:28.523 回答