使用Breeze手动应用OData查询选项,并且仍返回InlineCount [英] Manually applying OData query options using Breeze and still return InlineCount

查看:85
本文介绍了使用Breeze手动应用OData查询选项,并且仍返回InlineCount的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Breeze与Web API后端一起使用,我试图弄清楚如何正确地应用接收到的OData查询选项,同时还尝试返回给定OData查询的inlineCount。



之所以这样做,是因为我需要点击另一个数据源,并在返回的实体上填充一些关联的属性(同时仍然允许分页和排序) 。



以下是我要执行的操作示例:

  [HttpGet] 
公共QueryResult预订(ODataQueryOptions选项){
var set = _contextProvider.Context.Reservations.Where(r => r是ScheduledReservation || r是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,sureStableOrdering = true,HandleNullPropagation = HandleNullPropagationOption.Default});

//在此处点击其他数据源,并在返回的实体上填写相关属性

返回新的QueryResult
{
InlineCount = //想要轻而易举地执行此查询,
结果= queryable.Cast< Reservation>()
};
}



如何在仍然可查询的状态下手动将查询选项应用于我的Queryable允许Breeze照顾执行并返回inlineCount吗?



我在上面所做的问题有三个方面: / strong>



1)这会引发错误,表明Breeze无法创建EDM模型,因为此操作方法返回QueryResult而不是实现IEnumerable<>的东西。 / p>

2)我使用的是Breeze的ApplyQuery()方法,而不是ODataQueryOptions对象的ApplyTo(),因为我还想对ApplyTo()的嵌套属性进行排序尚未允许。我不确定是否通过挖掘Breeze的功能并手动调用ApplyQuery()方法来超越自己的界限。



3)我可以在EF中看到调用Breeze的ApplyQuery方法时正在执行Profiler,以检索inlineCount的查询。我发现ApplyQuery方法将ODataQueryOptions对象上的inlineCount设置为具有MS_InlineCount键的属性。因此,似乎没有一种简单的方法来实现这一目标(相反,对我来说,将其从ODataQueryOptions属性数组中拉出来似乎是个骗局)。



我要问的首要原因是整个思路都令人尴尬。我想仔细检查一下,确保没有错过让我轻松地将OData查询应用于我的DbSet并仍然允许返回inlineCount(并且仍然允许正确的分页和排序)的东西。

解决方案

我最终偶然发现了该问题的解决方案。以下帖子使我找到了解决上述问题的正确解决方案。



微风WebAPI:如何将QueryResult与ODataQueryOptions结合以返回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

这篇关于使用Breeze手动应用OData查询选项,并且仍返回InlineCount的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆