如何在链接实体上使用过滤器?

问题描述 投票:0回答:5

我正在使用 crm 2016 并尝试根据链接实体过滤获取产品(自定义实体)记录,我需要获取具有活动产品状态且 linked 电话类别为

fun
not open
:

的所有产品

产品 - (产品状态 = 活动) & (linkedphonecallcategory = 有趣 && linkedphonecallstatus != 打开)

当我运行当前查询时,我得到的结果没有链接实体过滤器。我不明白为什么。

这是我的代码:

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();
c# filter dynamics-crm dynamics-crm-2016 query-expressions
5个回答
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);

0
投票

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

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


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();

0
投票

更改线路:

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

致:

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

0
投票

我只是想分享您预期查询的这种可读格式。也像提到的其他答案一样,使用 XrmToolBox 中的 Advanced find 或 fetchXML builder 来构建 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")
                }
            }
        }
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.