简体   繁体   中英

Manually applying OData query options using Breeze and still return InlineCount

I'm using Breeze with a Web API back-end and I am trying to figure out how to properly apply the received OData query options while also trying to return the inlineCount for the given OData query.

The reason I'm trying to do this is because I need to hit another datasource and fill in some associated properties on the entity that I'm returning (all while still allowing for paging and sorting).

Here's an example of what I'm trying to do:

[HttpGet]
public QueryResult Reservations( ODataQueryOptions options ) {    
    var set = _contextProvider.Context.Reservations.Where(r => r is ScheduledReservation || r is PoolReservation).Include(i => i.ReservationType)
                                      .Include(i => i.Vehicle)
                                      .Include(i => i.Vehicle.VehicleMake)
                                      .Include(i => i.Vehicle.VehicleModel).AsQueryable();

    var queryable = Breeze.WebApi.QueryHelper.ApplyQuery(set, options, new ODataQuerySettings { EnableConstantParameterization = true, EnsureStableOrdering = true, HandleNullPropagation = HandleNullPropagationOption.Default });

    // Hit other data source here and fill in associated properties on returned entities

    return new QueryResult
    {
        InlineCount = // Would like to get at breeze's execution of this query,
        Results = queryable.Cast<Reservation>()
    };
}

How do I manually apply the query options to my Queryable while still allowing for Breeze to take care of executing and returning an inlineCount?

The problem with what I'm doing above is three-fold:

1) This is throwing an error saying that Breeze cannot create an EDM model because this action method returns a QueryResult and not something that implements IEnumerable<>.

2) I'm using Breeze's ApplyQuery() method instead of the ApplyTo() off of the ODataQueryOptions object because I am also wanting to sort on nested properties which ApplyTo() does not yet allow. I'm not sure if I'm over-stepping my bounds by digging into Breeze's functionality and manually calling the ApplyQuery() method.

3) I can see in EF Profiler that the query to retrieve the inlineCount is executing when calling Breeze's ApplyQuery method. I have found that the ApplyQuery method is setting the inlineCount on the ODataQueryOptions object as a property with a key of MS_InlineCount. So, it doesn't appear that there is an "easy" way to get at that (rather, it seems hokey for me to go pull it out of the ODataQueryOptions properties array).

The over-arching reason why I'm asking is this whole line of thinking feels awkward. I want to double-check and make sure I have not missed something that would allow me to easily apply the OData query to my DbSet and still allow for returning the inlineCount (and still allow for proper paging and sorting).

I eventually stumbled upon the solution to this problem. The following post is what lead me to the proper solution to take care of the problem I described above.

Breeze WebAPI: How to combine QueryResult with ODataQueryOptions to return inlineCount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM