OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
853 stars 476 forks source link

PageResult example not working. Is a cast missing? #1525

Open NetTecture opened 6 years ago

NetTecture commented 6 years ago

Current 7.0 Nuget. This contains a number of correlated issues.

The following code:

        var objectList = query.ToList();
        var results = (IQueryable) options.ApplyTo(objectList.AsQueryable());
        return new PageResult<Api.Odata.Tenant>(results as IEnumerable<Api.Odata.Tenant>, new System.Uri("http://blabla/"), 55);

results in an exception as the PageResult first line is null (results is NOT an IEnumerable. This is based on example code, but with dotnetcore etc. it is hard to remember where that came from. Documentation is definitely lacking.

var results if of type

  Name Value Type
results {System.Collections.Generic.List1[Api.Odata.Tenant].Select(Param_0 => new SelectAllAndExpand1() {ModelID = "ce35c6b0-2c6d-44b7-9ff8-51bed4c03018", Instance = Param_0, UseInstanceForProperties = True, Container = new NamedProperty1() {Name = "Buildings", Value = IIF((IIF((Param_0 == null), null, Param_0.Buildings) == null), null, IIF((Param_0 == null), null, Param_0.Buildings).Select(Param_1 => new SelectAll1() {ModelID = "ce35c6b0-2c6d-44b7-9ff8-51bed4c03018", Instance = Param_1, UseInstanceForProperties = True}))}})} System.Linq.IQueryable {System.Linq.EnumerableQuery<Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder.SelectAllAndExpand>}

The funny thing is that this SHOULD be castable to Api.Odata.Tenant - it is not.

Changing

var results = (IQueryable) options.ApplyTo(objectList.AsQueryable());

To

var results3 = (IQueryable)options.ApplyTo(objectList.AsQueryable());

results in

System.InvalidCastException HResult=0x80004002 Message=Unable to cast object of type 'System.Linq.EnumerableQuery1[Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder+SelectAllAndExpand1[Api.Odata.Tenant]]' to type 'System.Linq.IQueryable1[Api.Odata.Tenant]'. Source=Api.Odata.Web StackTrace: at Api.Odata.Web.Controllers.TenantController.Get(ODataQueryOptions1 options) in C:\Work\OBB\Source.Backend\Api.Odata.Web\Controllers\TenantController.cs:line 31 at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()

As does casting it to IEnumerable of type

System.InvalidCastException HResult=0x80004002 Message=Unable to cast object of type 'System.Linq.EnumerableQuery1[Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder+SelectAllAndExpand1[Api.Odata.Tenant]]' to type 'System.Collections.Generic.IEnumerable1[Api.Odata.Tenant]'. Source=Api.Odata.Web StackTrace: at Api.Odata.Web.Controllers.TenantController.Get(ODataQueryOptions1 options) in C:\Work\OBB\Source.Backend\Api.Odata.Web\Controllers\TenantController.cs:line 31 at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()

There is no indication what the proper way is to move from this returned query to something I can enter into Page result.

What does make this worse is the bad exception in the constructor if PageResult. There isa null check in the constructor that reads:

        if (items == null)
        {
            throw Error.ArgumentNull("data");
        }

as the parameter is called items - this results in a bad exception. This should read:

        if (items == null)
        {
            throw Error.ArgumentNull("items");
        }
NetTecture commented 6 years ago

Anyone cares about those? Because right now the ONLY way to get OData working with Entity Framework Core is for me to pull all data into memory and then hand that over to Odata - which is like the antithesis of how you should use sql server.

xiaotupansy commented 5 years ago

I'm facing the same problem! Hope someone here can help!

albertwoo commented 4 years ago

I am facing this issue too 🙁