简体   繁体   English

使用Breeze手动应用OData查询选项,并且仍返回InlineCount

[英]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. 我正在将Breeze与Web API后端一起使用,并且试图弄清楚如何正确应用接收到的OData查询选项,同时还尝试返回给定OData查询的inlineCount。

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? 如何在仍允许Breeze负责执行和返回inlineCount的同时将查询选项手动应用于Queryable?

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<>. 1)这引发错误,表明Breeze无法创建EDM模型,因为此操作方法返回QueryResult而不是实现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. 2)我使用Breeze的ApplyQuery()方法而不是ODataQueryOptions对象的ApplyTo(),因为我还想对ApplyTo()尚不允许的嵌套属性进行排序。 I'm not sure if I'm over-stepping my bounds by digging into Breeze's functionality and manually calling the ApplyQuery() method. 我不确定是否通过挖掘Breeze的功能并手动调用ApplyQuery()方法来超越自己的界限。

3) I can see in EF Profiler that the query to retrieve the inlineCount is executing when calling Breeze's ApplyQuery method. 3)我可以在EF Profiler中看到调用Breeze的ApplyQuery方法时正在执行检索inlineCount的查询。 I have found that the ApplyQuery method is setting the inlineCount on the ODataQueryOptions object as a property with a key of MS_InlineCount. 我发现ApplyQuery方法将ODataQueryOptions对象上的inlineCount设置为具有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). 因此,似乎没有一种“简单”的方法可以做到这一点(相反,对于我来说,将其从ODataQueryOptions属性数组中拉出来似乎是很关键的)。

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). 我想仔细检查一下,确保没有错过让我轻松地将OData查询应用于我的DbSet并仍然允许返回inlineCount的东西(并且仍然允许正确的分页和排序)。

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 Breeze WebAPI:如何将QueryResult与ODataQueryOptions组合以返回inlineCount

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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