0

在微风控制器中:

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

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

4

1 回答 1

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 的值并添加代码以将其传递给过滤器

或者手动完成所有服务器端的微风工作:

[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

于 2013-08-26T21:06:31.273 回答