首页 文章

Microsoft CRM SDK使用Guid作为查询条件值

提问于
浏览
2

我需要使用Mircosoft CRM SDK根据存储在每个地址实体中的 parentid 来提取一组地址 . 这 parentid 自然对应于父记录的 GUID .

我已经用两种方式构建了这个查询:首先我只是将 GUID 放在 condition.Values 属性中,然后我尝试使用 EntityReference .

如果我只是使用 GUID 查询完成但我没有收到任何结果 . 如果我使用 EntityReference ,我会得到以下异常:

尝试序列化参数http://schemas.microsoft.com/xrm/2011/Contracts/Services:query时出错 . InnerException消息是'Type'Microsoft.Xrm.Sdk.EntityReference',数据 Contract 名称为'EntityReference:http://schemas.microsoft.com/xrm/2011/Contracts'不是预期的 . 考虑使用DataContractResolver或将任何静态未知的类型添加到已知类型列表中

我也尝试使用两种不同的方法构建查询,但是我在条件值的相同行中得到了相同的结果 GUID .

如果我打开我正在处理的帐户的Microsoft Dynamics网页,我可以清楚地看到已输入该地址 .

enter image description here

你怎么样包括 GUID 来查询 parentid

Method One

ConditionExpression condition = new ConditionExpression();
    condition.AttributeName = "parentid";
    condition.Operator = ConditionOperator.Equal;

    //This works but returns no results
    condition.Values.Add(new EntityReference("account", id));

    //This throws the above exception
    //condition.Values.Add(new EntityReference("account", id));

    ColumnSet column = new ColumnSet(true);
    QueryExpression query = new QueryExpression();
    query.ColumnSet = column;
    query.EntityName = "customeraddress";
    query.Criteria.AddCondition(condition);

Method Two

QueryExpression queryEx = new QueryExpression
            {
                EntityName = "customeraddress",
                ColumnSet = new ColumnSet(true),
                Criteria =
                    {
                        FilterOperator = LogicalOperator.And,
                        Conditions =
                            {
                                new ConditionExpression
                                    {
                                        AttributeName = "parentid",
                                        Operator = ConditionOperator.Equal,

                                        //Works but returns no results
                                        Values = {id}

                                        //Throws exception
                                        //Values = {new EntityReference("account", id)}
                                    }
                            }
                    }
            };

2 回答

  • 1

    提前查询地址查找:

    enter image description here

    如果这导致一些记录,那么简单查询下面应该有u的数据 .

    private EntityCollection getAddresses(Guid AccountID, IOrganizationService service)
    {
        QueryExpression query = new QueryExpression("customeraddress");
        query.ColumnSet = new ColumnSet(true);
        query.Criteria.AddCondition("parentid", ConditionOperator.Equal, AccountID);    
        EntityCollection results = service.RetrieveMultiple(query);
        return results;
    }
    
  • 0

    另一种获取此数据的方法是从Advanced Find下载该Query的FetchXML,然后执行该Fetch Expression并将Id添加为字符串:
    enter image description here

    string fetchxml= @"  <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
          <entity name="customeraddress">
            <attribute name="name" />
            <attribute name="line1" />
            <attribute name="city" />
            <attribute name="postalcode" />
            <attribute name="telephone1" />
            <attribute name="customeraddressid" />
            <order attribute="name" descending="false" />
            <link-entity name="account" from="accountid" to="parentid" alias="aa">
              <filter type="and">
                <condition attribute="accountid" operator="eq" uitype="account" value="{7EDECF7F-AE71-E511-80EC-3863BB3610E8}" />
              </filter>
            </link-entity>
          </entity>
        </fetch>";
    
      FetchExpression fetch = new FetchExpression(fetchxml);
      EntityCollection fetchresults = service.RetrieveMultiple(fetch);
    

    然后,您可以使用guid字符串替换fetchxml字符串中的Guid值 .

相关问题