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

C# Microsoft CRM SDK使用Guid作为查询条件值,c#,.net,dynamics-crm-2011,dynamics-crm,crm,C#,.net,Dynamics Crm 2011,Dynamics Crm,Crm,我需要使用Mircosoft CRM SDK根据存储在每个地址实体中的parentid提取一组地址。这个parentid自然地对应于父记录的GUID 我以两种方式构建了这个查询:首先,我只需将GUID放在条件.Values属性中,然后我尝试使用andEntityReference 如果我只是使用GUID查询就完成了,但没有收到任何结果。如果我使用EntityReference我会得到以下异常: 尝试序列化参数时出错。InnerException消息为“Type”Microsoft.Xrm.Sdk

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

我以两种方式构建了这个查询:首先,我只需将
GUID
放在
条件.Values
属性中,然后我尝试使用and
EntityReference

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

尝试序列化参数时出错。InnerException消息为“Type”Microsoft.Xrm.Sdk。不应使用数据协定名称“EntityReference:”的EntityReference。考虑使用DATACONTRORTCORDEVER或在已知类型

列表中添加静态不知道的任何类型。 我还尝试使用两种不同的方法构建查询,但是在条件值的
GUID
输入的相同行上得到了相同的结果

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

如何将
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)}
                                    }
                            }
                    }
            };

提前查询地址查找:

如果这导致了一些记录,那么下面的简单查询应该包含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;
}

获取此数据的另一种方法是从Advanced Find下载该查询的FetchXML,然后执行该fetch表达式并将Id添加为字符串:

string fetchxml=@”
";
FetchExpression fetch=新的FetchExpression(fetchxml);
EntityCollection fetchresults=service.RetrieveMultiple(提取);

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

您是否尝试过简单的
QueryByAttribute
?直接使用Guid或
EntityReference
获得完全相同的结果。我想除此之外,我唯一能想到的是1)三次检查您传递的ID实际上是这些CustomerAddress记录的ParentID,以及2)确保您运行的上下文具有查询这些CustomerAddress记录的权限(并且阻止您返回结果的不是安全性)。祝你好运
  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);