Manually applying OData query options using Breeze and still return InlineCount

心不动则不痛 提交于 2019-12-12 14:57:18

问题


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).


回答1:


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



来源:https://stackoverflow.com/questions/18705932/manually-applying-odata-query-options-using-breeze-and-still-return-inlinecount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!