【问题标题】:Retrieve attribute value of link-entity in FetchXML在 FetchXML 中检索链接实体的属性值
【发布时间】:2019-09-24 15:17:48
【问题描述】:

我有以下代码:

    Entity account = service.Retrieve("account", ID, new ColumnSet(true));
    string name = (string)account.Attributes["name"];

        const string fetchXmlPattern =
        @"<fetch mapping='logical' version='1.0'>
            <entity name='account'>
                <attribute name='maintname' />
            <filter>
                <condition attribute='maintname' operator='eq' value='{0}' />
            </filter>
            <link-entity name='contact' from='contactid' to='primarycontactid' link-type='inner'>
                <attribute name='accountidname'/>
            </link-entity>
            </entity>
        </fetch>";

        string fetchXml = string.Format(fetchXmlPattern, name);

        var fetchExpression = new FetchExpression(fetchXml);
        EntityCollection response = service.RetrieveMultiple(fetchExpression);
        var records = response.Entities;

        List<Account> Accounts= new List<Account>();

        foreach (var item in records)
        {
            Account AccountFranchisee = new Account();
            AccountFranchisee.ID = (Guid)item.Attributes["accountid"];
            AccountFranchisee.Name = (string)item.Attributes["accountidname"];
            Accounts.Add(AccountFranchisee);
        }
        return Accounts;

我在这一行得到一个 Key Not Found 异常:

AccountFranchisee.Name = (string)item.Attributes["accountidname"];

意味着它找不到链接实体中的属性。我尝试在互联网上放置别名,更改模式,更改语法和几种解决方案等...我花了很多时间在这件事上,仍然无法弄清楚哪里出了问题。

如何获得价值?

谢谢!

【问题讨论】:

    标签: c# asp.net-mvc fetchxml


    【解决方案1】:

    accountidname 是链接实体的属性。因此,您需要使用Aliased 值来检索其值。

    首先,修改您的 FetchXml 为 link-entity 指定一个 alias。在这里,我给出了“PrimaryContact”的别名——这一步并不是强制性的,但我认为最好是明确的。如果您不指定别名,CRM 会自动为您创建一个(可能是“contact1”)

    <fetch mapping='logical' version='1.0'>
      <entity name='account'>
        <attribute name='maintname' />
        <filter>
          <condition attribute='maintname' operator='eq' value='{0}' />
        </filter>
        <link-entity name='contact' from='contactid' to='primarycontactid' link-type='inner' alias='PrimaryContact'>
          <attribute name='accountidname'/>
        </link-entity>
      </entity>
    </fetch>
    

    现在,当您检索值时,您需要结合别名和字段名称 尝试类似

    AccountFranchisee.Name = item.GetAttributeValue<AliasedValue>("PrimaryContact.accountidname").Value;
    

    【讨论】:

    • 很遗憾,我无法测试这个,因为我在我开发的公司的实习期结束了^^ 但在写我在另一个主题上发现的问题之前,我确实测试了类似的东西。如果我没记错的话,当有人告诉我必须在现有视图上而不是在实际数据库表上完成这些提取是完全合乎逻辑的时,我解决了这个问题。由于在视图链接实体已经完成,没有更多的问题!尽管如此,还是感谢您的帮助! :)
    猜你喜欢
    • 2020-10-16
    • 2019-04-12
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多