C#CRM QueryExpression LinkEntity通过多个字段联接 [英] C# CRM QueryExpression LinkEntity join via multiple field
问题描述
在CRM中是否可以动态加入多个字段?
Is it possible to join multiple fields dynamically in CRM?
我的意思是在SQL中是这样的
What I mean is something like this in 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个字段连接起来。我的实际代码在下面,减去我无法提供的部分。
I'm trying to come-up with something above using queryexpression but I'm not getting the desired behavior I want and I end having LOTS of records than expected. I can do this if I KNOW before hand the value of field number 2, but what at runtime I don't know and the query has to join 2 fields. My actual code is below, minus the part that I can't come up with.
这不起作用...我期望只有2条记录,但我得到数百...
This doesn't work... I'm expecting only 2 records but I get hundreds...
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);
我在做什么错了?
推荐答案
我不确定您是否对此进行了研究,但是在Linq中进行联接真的很容易( msdn示例)。我已经这样写了Contact-Accounts联接:
I'm not sure if you looked into it, but joins are really easy in Linq (msdn examples). I've written Contact-Accounts joins like this:
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());
这篇关于C#CRM QueryExpression LinkEntity通过多个字段联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!