l--*_*''' 9 c# odata dynamics-crm-2011 asp.net-core
如何通过guid正确过滤我的数据集?
我正在暴露odata端点,并尝试导航到URL:
http:// localhost:5001/mystuf/api/v2/AccountSet?$ filter = AccountId%20eq%20guid%2703a0a47b-e3a2-e311-9402-00155d104c22%27
当我的odata端点尝试过滤该guid上的数据集时,我得到:
Run Code Online (Sandbox Code Playgroud)"message": "Invalid 'where' condition. An entity member is invoking an invalid property or method.", "type": "System.NotSupportedException" { "odata.error": { "code": "", "message": { "lang": "en-US", "value": "An error has occurred." }, "innererror": { "message": "Invalid 'where' condition. An entity member is invoking an invalid property or method.", "type": "System.NotSupportedException", "stacktrace": " at Microsoft.Xrm.Sdk.Linq.QueryProvider.ThrowException(Exception exception) at Microsoft.Xrm.Sdk.Linq.QueryProvider.FindValidEntityExpression(Expression exp, String operation) at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhereCondition(BinaryExpression be, FilterExpressionWrapper parentFilter, Func`2 getFilter, Boolean negate) at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhere(String parameterName, BinaryExpression be, FilterExpressionWrapper parentFilter, Func`2 getFilter, List`1 linkLookups, Boolean negate) at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhereBoolean(String parameterName, Expression exp, FilterExpressionWrapper parentFilter, Func`2 getFilter, List`1 linkLookups, BinaryExpression parent, Boolean negate) at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhere(QueryExpression qe, String parameterName, Expression exp, List`1 linkLookups) at Microsoft.Xrm.Sdk.Linq.QueryProvider.GetQueryExpression(Expression expression, Boolean& throwIfSequenceIsEmpty, Boolean& throwIfSequenceNotSingle, Projection& projection, NavigationSource& source, List`1& linkLookups) at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](Expression expression) at Microsoft.Xrm.Sdk.Linq.QueryProvider.GetEnumerator[TElement](Expression expression) at Microsoft.Xrm.Sdk.Linq.Query`1.GetEnumerator() at Microsoft.Xrm.Sdk.Linq.Query`1.System.Collections.IEnumerable.GetEnumerator() at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__24.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.Owin.HttpMessageHandlerAdapter.<BufferResponseContentAsync>d__13.MoveNext()" } } }
CSDL文件的开头看起来像这样:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Schema Namespace="Xrm" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/edm">
<EntityType Name="Account">
<Key>
<PropertyRef Name="AccountId" />
</Key>
<Property Name="TerritoryCode" Type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue" Nullable="false" />
<Property Name="LastUsedInCampaign" Type="Edm.DateTime" Nullable="true" />
...
Run Code Online (Sandbox Code Playgroud)
被触发的控制器是:
public IHttpActionResult Get(ODataQueryOptions<Account> options)
{
var retval = options.ApplyTo(_accountService.GetAccountSet());
return Ok(retval);
}
Run Code Online (Sandbox Code Playgroud)
以上Get方法将过滤器应用于:
public IQueryable<Account> GetAccountSet()
{
return _xrmServiceContext.AccountSet;
}
Run Code Online (Sandbox Code Playgroud)
如何通过guid正确过滤我的数据集?
请注意,当我返回时:
return _xrmServiceContext.AccountSet.Take(2);
Run Code Online (Sandbox Code Playgroud)
然后将数据正确返回给客户端(序列化/反序列化正确工作); 但是,根本不应用过滤器.
根据更新的文档解决方案取决于AccountId字段的类型.
如果AccountId类型是Guid(很可能是你的情况),那么与guid文字进行比较应该没有guid关键字或将其写入引号.所以有效查询应如下所示:
AccountId eq 03a0a47b-e3a2-e311-9402-00155d104c22
Run Code Online (Sandbox Code Playgroud)
结果url将是这样的:
HTTP://本地主机:5001/mystuf/API/V2/AccountSet $过滤= ACCOUNTID%20当量%2003a0a47b-e3a2-e311-9402-00155d104c22
但是如果AccountIdtype是String并且它包含guid的字符串表示,则应该使用字符串文字的规则并将其放在单引号中,如下所示:
AccountId eq '03a0a47b-e3a2-e311-9402-00155d104c22'
Run Code Online (Sandbox Code Playgroud)
结果url将是这样的:
你可以测试两个网址,看看哪个适合你:)
| 归档时间: |
|
| 查看次数: |
1829 次 |
| 最近记录: |