我使用crm 2016并尝试根据linkentity过滤获取产品(自定义实体)记录,我需要获得所有具有Active productstatus和链接电话呼叫类别的产品fun
和not open
:
产品 - (productstatus = Active)& (linkedphonecallcategory = fun&& linkedphonecallstatus!= open)
当我运行当前查询时,我得到的结果没有链接实体过滤器。我不明白为什么。
这是我的代码:
FilterExpression filter1 = new FilterExpression(LogicalOperator.And);
filter1.Conditions.Add(new ConditionExpression("phonecallcategory", ConditionOperator.Equal, "fun"));
filter1.Conditions.Add(new ConditionExpression("statecode", ConditionOperator.NotEqual, 0));
LinkEntity phoneCallLink = new LinkEntity("product", "phonecall", "productid", "regardingobjectid", JoinOperator.LeftOuter);
phoneCallLink.LinkCriteria = filter1;
phoneCallLink.EntityAlias = "products";
QueryExpression query = new QueryExpression("product");
query.ColumnSet = new ColumnSet("productname");
query.LinkEntities.Add(phoneCallLink);
query.Criteria.AddCondition(new ConditionExpression("productstatus", ConditionOperator.Equal, 0));
EntityCollection AllProductsWithSpecificCallsNotOpen = new EntityCollection();
答案 0 :(得分:0)
我认为您的问题出在QueryExpression query = new QueryExpression("product");
您的问题是您的Product
是自定义实体,但您已使用不允许活动的系统实体Product
的架构名称。
您必须使用实体的逻辑(全部小写)名称
您也可以使用 XrmToolbox 生成此代码。这是我使用此工具生成的示例查询。请注意:
Contact
代替Product
代码:
// Initialise
var QEcontact = new QueryExpression("contact");
// Add columns to QEcontact.ColumnSet
QEcontact.ColumnSet.AddColumns("firstname");
// Add link-entity QEcontact_phonecall
var QEcontact_phonecall = QEcontact.AddLink("phonecall", "contactid", "regardingobjectid");
// Define filter QEcontact_phonecall.LinkCriteria
QEcontact_phonecall.LinkCriteria.AddCondition("category", ConditionOperator.Equal, "fun");
QEcontact_phonecall.LinkCriteria.AddCondition("statuscode", ConditionOperator.NotEqual, 1);
答案 1 :(得分:0)
我只想分享您期望的查询的可读格式。也像jasonscript提到的那样,在xrmtoolbox中使用Adv Find或FetchXML构建器来构建fetchxml查询,你可以查看与其本身等效的SQL和Query表达式。
QueryExpression query = new QueryExpression("product")
{
ColumnSet = new ColumnSet("productname"),
Criteria = new FilterExpression(LogicalOperator.And)
{
Conditions =
{
new ConditionExpression("productstatus", ConditionOperator.Equal, 0)
}
},
LinkEntities =
{
new LinkEntity("product", "phonecall", "productid", "regardingobjectid", JoinOperator.LeftOuter)
{
Columns = new ColumnSet("phonecallcategory"),
LinkCriteria = new FilterExpression(LogicalOperator.And)
{
Conditions =
{
new ConditionExpression("statecode", ConditionOperator.NotEqual, 0),
new ConditionExpression("phonecallcategory", ConditionOperator.Equal, "fun")
}
}
}
}
};
答案 2 :(得分:0)
尝试改用FetchXml查询。使用这种方法,您可以使用“高级查找”来创建过滤器。然后,在窗口中下载FetchXml。到目前为止,调试起来更加容易。
答案 3 :(得分:0)
您可以看看这里,我已经重新组织了您的查询以使其易于理解
示例:QueryExpression 和 LinkEntity
//Link
LinkEntity phoneCallLink = new LinkEntity();
phoneCallLink.LinkFromEntityName = "new_product";
phoneCallLink.LinkToEntityName = "phonecall";
phoneCallLink.LinkFromAttributeName = "new_productid";
phoneCallLink.LinkToAttributeName = "regardingobjectid";
phoneCallLink.JoinOperator = JoinOperator.LeftOuter;
phoneCallLink.EntityAlias = "phone";
phoneCallLink.LinkCriteria.Conditions.Add(new ConditionExpression("statecode", ConditionOperator.NotEqual, 0));
phoneCallLink.LinkCriteria.Conditions.Add(new ConditionExpression("phonecallcategory", ConditionOperator.Equal, "fun"));
//Query
QueryExpression query = new QueryExpression("new_product");
query.ColumnSet = new ColumnSet("new_name");
query.LinkEntities.Add(phoneCallLink);
query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));
//Execute
var AllProductsWithSpecificCallsNotOpen = service.RetrieveMultiple(query);
如果这仍然不起作用,您可以构建并导出 fetchXml
查询并将其转换为查询表达式:
示例:将 FetchXml 转换为 QueryExprssion
string fetchXml =
@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='accountid' />
<order attribute='createdon' descending='true' />
<order attribute='modifiedon' descending='true' />
<filter type='and'>
<condition attribute='createdon' operator='this-month' />
</filter>
</entity>
</fetch>";
// Convert the FetchXML into a query expression.
var conversionRequest = new FetchXmlToQueryExpressionRequest();
conversionRequest.FetchXml = fetchXml; //fetchXml string
var conversionResponse =(FetchXmlToQueryExpressionResponse)service.Execute(conversionRequest);
var result = conversionResponse.Results.FirstOrDefault();
答案 4 :(得分:0)
换行:
LinkEntity phoneCallLink = new LinkEntity("product", "phonecall", "productid", "regardingobjectid", JoinOperator.LeftOuter);
致:
LinkEntity phoneCallLink = new LinkEntity("product", "phonecall", "productid", "regardingobjectid", JoinOperator.Inner);