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

时间:2014-12-16 17:50:08

标签: c# .net dynamics-crm-2011 dynamics-crm crm

我需要使用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',数据协定名称为'EntityReference:http://schemas.microsoft.com/xrm/2011/Contracts'不是预期的。考虑使用DataContractResolver或将任何静态未知的类型添加到已知类型列表

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

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

enter image description here

如何通过GUID查询parentid

方法一

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

方法二

        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 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

获取此数据的另一种方法是从高级查找下载该查询的FetchXML,然后执行该获取表达式并将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值。