Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI, oData and RavenDb - throws an error for $top and $skip

I am using WebAPI to return a collection of basic Supplier objects from RavenDb

The Supplier class is:

public class Supplier
{
    public string Id { get; set; }
    public string Title { get; set; }
}

The WebAPI method is:

[Queryable]
public IQueryable<Supplier> Get()
{
    using (var session = _store.OpenSession())
    {
        return session.Query<Supplier>();
    }
}

When I call http://localhost:8083/api/suppliers?$orderby=Id%20desc or http://localhost:8083/api/suppliers?$filter=Title%20eq%20'Test' everything works fine. However, whenever I use $top or $skip I get an exception:

Unable to cast object of type 'System.Linq.Expressions.PropertyExpression' to type 'System.Linq.Expressions.ConstantExpression'.

If I return the whole collection into memory using ToList() it works fine, so it looks like a problem with the deferred execution of the query with RavenDb.

I am using the latest stable release of RavenDb.Client 2.0.2.2261.

Has anyone else had this problem or have a resolution to it?

The Stack Trace is:

   at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitQueryableMethodCall(MethodCallExpression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 994
   at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitMethodCall(MethodCallExpression expression, Boolean negated) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 693
   at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitExpression(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 140
   at Raven.Client.Linq.RavenQueryProviderProcessor`1.GetLuceneQueryFor(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 1318
   at Raven.Client.Linq.RavenQueryProviderProcessor`1.Execute(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 1352
   at Raven.Client.Linq.RavenQueryProvider`1.Execute(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProvider.cs:line 135
   at Raven.Client.Linq.RavenQueryProvider`1.System.Linq.IQueryProvider.Execute(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProvider.cs:line 190
   at Raven.Client.Linq.RavenQueryInspector`1.GetEnumerator() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryInspector.cs:line 99
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper(Object list)
   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)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.<WriteToStreamAsync>b__c()
   at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)
like image 680
valps Avatar asked Mar 10 '13 04:03

valps


1 Answers

I was able to repro your issue and was successfully able to validate my initial guess as to what could be going wrong. Try [Queryable(EnableConstantParameterization = false)] instead of plain [Queryable]. Late in the OData development cycle we made a perf change to make optimal use of EF automatic compilation of LINQ queries. We enable it by default as it is a safe change. Unfortunately, I think RavenDB LINQ provider is not able to understand the LINQ queries generated with this optimization. The code that I shared turns it off and produces vanilla LINQ queries.

like image 108
RaghuRam Nadiminti Avatar answered Oct 26 '22 10:10

RaghuRam Nadiminti