2016-09-04 34 views
9

Veri kümemi nasıl yönlendiririm?Veri kümemi oda tabanını kullanarak doğru şekilde nasıl filtreleyebilirim?

Bir OData bitiş noktası teşhir ve URL'ye çalışıyorum:?

http://localhost:5001/mystuf/api/v2/AccountSet $ filtresi = accountid% 20eq% 20guid% 2703a0a47b-e3a2-e311-9402-00155d104c22% 27

benim OData son nokta bu guid üzerinde veri kümesini filtrelemek çalıştığında

, ben alıyorum:

"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 dosya tuvaletten başlangıç Böyle ks şey:

public IHttpActionResult Get(ODataQueryOptions<Account> options) 
{ 
    var retval = options.ApplyTo(_accountService.GetAccountSet()); 

    return Ok(retval); 

} 

ve yukarıdaki Get yöntemi süzgeci uygular:

<?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" /> 
... 

kontrolör tetiklenir oluyor ise eğer doğru nasıl filtre

public IQueryable<Account> GetAccountSet() 
{ 
    return _xrmServiceContext.AccountSet; 
} 

veri kümemi rehbere göre mi?

yerine bu döndüğünüzde lütfen unutmayın: Sonra veri istemciye doğru döndürülür

return _xrmServiceContext.AccountSet.Take(2); 

(seri/seri kaldırma düzgün çalışır); Bununla birlikte, filtre hiç uygulanmamaktadır.

+0

Kişisel OData sorgu yanlıştır görmek için her iki url test edebilirsiniz. Bir filtre için sorgu yapısı şöyledir: $ filter = property eq 'value', $ filter = property eq property 'value'. $ Filter = AccountId eq 'guid value' öğesini deneyin. –

+0

"URI'de belirtilen sorgu geçerli değil. Uyumsuz türlere sahip bir ikili işleci algılandı." Edm.Guid "ve" Edm.String "işlenen türleri için" Equal "operatörü türü bulundu." }, –

+0

/api/v2/AccountSet? $ Filtre = AccountId eq '03a0a47b-e3a2-e311-9402-00155d104c22' –

cevap

6

updated documentation çözümüne göre, AccountId alanının türüne bağlıdır.

AccountId tip guid anahtar kelime olmadan yazılmalıdır literal veya tırnak içine koyarak guid ile karşılaştırıldığında daha (büyük olasılıkla davanız) Guid ise.

AccountId eq 03a0a47b-e3a2-e311-9402-00155d104c22 

Ve sonuç url bu olacaktır: Yani geçerli bir sorgu gibi görünmelidir

http://localhost:5001/mystuf/api/v2/AccountSet?$filter=AccountId%20eq%2003a0a47b-e3a2-e311-9402-00155d104c22

Ama AccountId tip String olup, guid dize gösterimini, sizin için kuralları kullanmalısınız içeriyorsa dize değişmez ve bu gibi tek tırnak içine koyun:

AccountId eq '03a0a47b-e3a2-e311-9402-00155d104c22' 

Ve sonuç URL'si şu olacaktır:

http://localhost:5001/mystuf/api/v2/AccountSet?$filter=AccountId%20eq%20%2703a0a47b-e3a2-e311-9402-00155d104c22%27

sadece bir sizin için çalışacaktır hangi :)

+0

'GuidValue', örnekteki alanın ismidir. guid literals hakkında daha fazla bilgi için lütfen şu adresteki cevapları okuyunuz: dataId eq 03a0a47b-e3a2-e311-9402-00155d104c22'. –

İlgili konular