Breezejs内联计数

时间:2013-08-26 11:07:03

标签: asp.net-mvc asp.net-mvc-4 odata breeze

在BreezeController中:

public IQueryable<Entities> Index()
{
    return this.context.entities.Where(e => e.value > 100);
}

Breeze查询query.inlineCount(true)返回Where之后的计数,但是我如何在Where语句之前返回实体计数或手动设置计数到响应?我知道过滤器,但在我的任务中,我需要Where声明在行动中。

1 个答案:

答案 0 :(得分:1)

为我工作

[Breeze.WebApi.BreezeController]
public class MyBreezeController : System.Web.Http.ApiController
{
    private readonly Breeze.WebApi.EFContextProvider<MyDbContext> context
        = new Breeze.WebApi.EFContextProvider<MyDbContext>();

    [SetInlineCountFilter]
    [Breeze.WebApi.BreezeQueryable]
    public System.Linq.IQueryable<MyEntity> Index()
    {
        return this.context.Context.MyEntities.Where(e => e.Value > 100);
    }
}

public class SetInlineCountFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
    public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
    {
        var content = (System.Net.Http.ObjectContent)actionExecutedContext.Response.Content;
        var result = (Breeze.WebApi.QueryResult)content.Value;
        result.InlineCount = 42;
        base.OnActionExecuted(actionExecutedContext);
    }
}

现在,您可以从任何地方获取inlinecount的值,并添加代码以将其传递给过滤器

或者手动完成所有sevrer-side微风工作:

[Breeze.WebApi.BreezeController]
public class MyBreezeController : System.Web.Http.ApiController
{
    private readonly Breeze.WebApi.EFContextProvider<MyDbContext> context
        = new Breeze.WebApi.EFContextProvider<MyDbContext>();

    public QueryResult Index()
    {
        return new QueryResult
                   {
                       InlineCount = 42,
                       Results = this.context.Context.MyEntities.Where(e => e.Value > 100)
                   };
    }
}

从请求this.Request.RequestUri.Query

获取查询选项