我正在尝试从新的REST API中检索多个数据,但我有一个奇怪的问题.如果我在集合上使用$ expand,那么它就不起作用.
请求是:
GET [Oranization URL]/api/data/v8.0/accounts?$expand=contact_customer_accounts HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Run Code Online (Sandbox Code Playgroud)
并回应:
{
"error": {
"code": "",
"message": "Expansion of navigation properties isn\u2019t supported on entity collections.",
"innererror": {
"message": "Expansion of navigation properties isn\u2019t supported on entity collections.",
"type": "Microsoft.Crm.CrmHttpException",
"stacktrace": " at Microsoft.Crm.Extensibility.OData.CrmODataUtilities.ThrowIfSelectClauseHasNavigationProperty(SelectExpandClause selectExpandClause, Boolean isCalledFromEntitySet)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.RetrieveEdmEntityCollection(CrmODataExecutionContext context, String entityCollectionName, ODataQueryOptions queryOptions)\r\n at Microsoft.Crm.Extensibility.OData.EntityController.GetEntitySetInternal(String entitySetName, String castEntityName, CrmODataExecutionContext context, CrmEdmEntityObjectCollection crmEdmEntityObjectCollection, ODataQueryOptions queryOptions)\r\n at Microsoft.Crm.Extensibility.OData.EntityController.GetEntitySet(String entitySetName)\r\n at lambda_method(Closure , …Run Code Online (Sandbox Code Playgroud) crm odata dynamics-crm-online dynamics-crm-2016 dynamics-crm-webapi
Count()扫描所有元素,因此if (list.Count() == 1)如果enumerable包含很多元素,则效果不佳.
Single()如果没有一个元素,则抛出异常.使用try { list.Single(); } catch(InvalidOperationException e) {}是笨拙和低效的.
SingleOrDefault()如果有多个元素,则抛出异常,因此if (list.SingleOrDefault() == null)(假设TSource是引用类型)对大小大于1的可枚举不起作用.
我有一个通用方法SendHttpRequest<TRequest, TResponse>,它接受请求类型和响应类型作为其通用参数输入。响应类型可以是布尔值或表示响应的类。
我的任务是返回true方法内发出的 HTTP 请求是否成功并且TResponse是bool. 如果TResponse是不同类型,我需要将响应内容反序列化为TResponse对象。返回true会产生编译时错误。有没有办法让单个方法同时支持布尔和非布尔返回类型?
private async Task<TResponse> SendHttpRequest<TRequest, TResponse>(TRequest request)
{
using (var client = new HttpClient())
{
client.BaseAddress = "http://example.com/";
var response = await client.PostAsJsonAsync("api_path", request).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
throw new MyException(response);
}
if (typeof (TResponse) == typeof (bool))
{
return true; // Generates compile-time error
}
else
{
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<TResponse>(content);
}
}
}
Run Code Online (Sandbox Code Playgroud)