1

我正在使用 crm 2016 并尝试根据链接性过滤获取产品(自定义实体)记录,我需要获取所有具有 Active productstatus 的产品并且链接电话类别为:funnot 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();
4

5 回答 5

0

你可以看看这里,我已经重组了你的查询,使其易于理解

示例:带有 LinkEntity 的 QueryExpression

//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();
于 2021-05-06T17:58:33.243 回答
0

换行:

LinkEntity phoneCallLink = new LinkEntity("product", "phonecall", "productid", "regardingobjectid", JoinOperator.LeftOuter);

至:

LinkEntity phoneCallLink = new LinkEntity("product", "phonecall", "productid", "regardingobjectid", JoinOperator.Inner);
于 2021-06-16T10:57:09.753 回答
0

我只想分享您预期查询的这种可读格式。也像提到的 jasonscript 一样,使用 xrmtoolbox 中的 Adv Find 或 FetchXML 构建器来构建 fetchxml 查询,您可以在那里查看等效的 SQL 和查询表达式。

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")
                }
            }
        }
    }
};
于 2018-05-04T03:21:06.607 回答
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);
于 2018-05-04T01:37:42.243 回答
0

尝试改用 FetchXml 查询。使用此方法,您可以使用高级查找来制作过滤器。然后,在窗口中下载 FetchXml。调试起来要容易得多。

https://msdn.microsoft.com/en-us/library/gg328332.aspx

于 2018-07-25T21:29:36.687 回答