使用类型参数的 RetrieveMultiple 不起作用

RetrieveMultiple using type parameter doesn't work

出于某种原因,以下函数有效:

public EntityCollection RetrieveMultipleByIds(IOrganizationService service, string entityLogicalName, string[] columnSet, string idFieldName, string[] guids)
{
    QueryExpression query = new QueryExpression(entityLogicalName);
    query.ColumnSet = new ColumnSet(columnSet);
    query.Criteria = new FilterExpression();
    query.Criteria.AddCondition(idFieldName, ConditionOperator.In, guids);
    EntityCollection entityCollection = service.RetrieveMultiple(query);
    return entityCollection;
}

但是这个 - 使用类型参数 - 不起作用:

public EntityCollection RetrieveMultipleByIds<T>(IOrganizationService service, string entityLogicalName, string[] columnSet, string idFieldName, T[] guids)
{
     QueryExpression query = new QueryExpression(entityLogicalName);
     query.ColumnSet = new ColumnSet(columnSet);
     query.Criteria = new FilterExpression();
     query.Criteria.AddCondition(idFieldName, ConditionOperator.In, guids);
     EntityCollection entityCollection = service.RetrieveMultiple(query);
     return entityCollection;
}

两者的调用方式相同(请注意,文字字符串 guid 仅适用于此示例,您可以放心我传递的 id 存在):

EntityCollection entityCollection =  
RetrieveMultipleByIds("org_session", new string[] { "org_sessionname" }, "org_sessionid", new string[] {"73A5794E-1662-E711-80FF-005056B74623"});

抛出的异常是:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:query. The InnerException message was 'Error in line 1 position 2826. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType' contains data from a type that maps to the name 'System:String[]'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'String[]' and namespace 'System'.'. Please see InnerException for more details.

我知道有类似的问题询问这种异常,但 none 与使用类型参数有关。如果有人遇到同样的问题并设法找到问题并解决它,我会很高兴听到。

尚不清楚您试图通过使方法 generic.You 仍然将字符串数组传递到您的 T[] 来完成什么。

由于您要返回 EntityCollection,您可以在第一个版本中查询任何实体以获得一组 GUID,只需传递不同的实体逻辑名称和 ID 字段即可。

如果你想使用代理 类 的早期绑定,你可以这样做(请注意这是未经测试的代码):

public List<T> RetrieveMultipleByIds<T>(IOrganizationService service, string entityLogicalName, string[] columnSet, string idFieldName, string[] guids)
{
     QueryExpression query = new QueryExpression(entityLogicalName);
     query.ColumnSet = new ColumnSet(columnSet);
     query.Criteria = new FilterExpression();
     query.Criteria.AddCondition(idFieldName, ConditionOperator.In, guids);
     var entityCollection = service.RetrieveMultiple(query);
     return entityCollection.Select(e => e.ToEntity<T>()).ToList();
}

然后调用它:

List<Account> accounts = RetrieveMultipleByIds<Account>(svc, "account", new string[] { "name" }, "accountid", new string[] {"73A5794E-1662-E711-80FF-005056B74623"});

query.Criteria.AddCondition(idFieldName, ConditionOperator.In, guids); 中,参数 guids 不能是通用类型。这只是一个序列化问题。为了安全起见,ConditionExpression.

中只允许使用 stringobject 类型的数组