网页API $带过滤器扩展的IQueryable [英] Web Api $extend IQueryable with filter

查看:154
本文介绍了网页API $带过滤器扩展的IQueryable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安装在那里我已经得到的WebAPI的OData服务返回:客户。在code返还客户是:

I have a setup where I've get a WebApi OData service which returns: Customers. The code for returning the customers is:

public IHttpActionResult GetCustomers(ODataQueryOptions<Customer> queryOptions)
{
    return Ok(context.Customers.Where(i => i.IsActive).AsQueryable());
}

所以GetCustomers的方法返回所有活跃客户的IQuerable结果。由于历史的目的,我们把所有的客户在数据库中,但是当一个客户被删除,我们isActive域设置为false。

So the GetCustomers method returns an IQuerable result of all active customers. For history purposes we leave all customers in the database, but when a customer is removed, we set the IsActive field to false.

该OData的安装使​​用简单builder.EntitySet打造的URL实体创建的。

The OData setup is created using a simple builder.EntitySet to build the Url for the entities.

EntitySetConfiguration<Customer> customers = builder.EntitySet<Customer>("customers");

这完美的作品。我有一个角前端,它使用$ HTTP调用接收客户等。

This works flawlessly. I have an Angular front-end which uses $http calls to receive the customers, etc.

不过客户可以包含数据库相关的联系人。为了获得在角前端的接触中,我使用$扩展的OData功能:

However a customer can contain related contacts in the database. To get the contacts in the Angular Frontend, I use the $extend functionality of OData:

odata/customers?$expand=contacts

这也是伟大工程。我收到的客户提供所有相关联系人。然而,当你已经猜到我想只接收具有IsActive应该返回联系人。和IQueryable的功能给了我所有的结果返回。

This also works great. I receive the customers with all related contacts. However as you've guessed I would like to receive only contacts which have IsActive should be returned. And the IQueryable functionality gives me all results back.

我明白我可以使用单独的OData调用来获取接触,但我真的想使用$扩展功能来获得一个调用的所有数据。我知道我还可以做在客户端(附:$过滤器)过滤。但是我想这个设置中正确的WebAPI部分,因此客户不必在意过滤不活动的结果返回。

I understand I can use the seperate Odata call to get the contacts, but I really would like to use the $expand features to get all data in one call. I know I can also do the filtering on the client side (with: $filter). But I'd like to setup this correctly in the WebApi part, so the client does not have to care about filtering inactive results back.

我似乎无法弄清楚如何正确地实现这一目标。有人可以帮我得到正确的轨道上吗?

I can't seem to figure out how to achieve this correctly. Can somebody help me get on the right track?

推荐答案

EntityFramework.DynamicFilters 是最大的工具,实体框架,我知道的。它跳进的经常要求的差距,但高达过滤 Incude 取值EF6从来没有实现过的功能。它斜靠在EF的拦截API和做繁重修改前pressions同时暴露了一个非常简单的界面。

EntityFramework.DynamicFilters is one of the greatest tools for Entity Framework that I know. It jumped into the gap of the often-requested but up to EF6 never implemented feature of filtered Incudes. It leans on EF's interception API and does the heavy lifting of modifying expressions while exposing a very simple interface.

在你的情况,你可以做的是这样的:

In your case, what you can do is something like this:

using EntityFramework.DynamicFilters;

// In OnModelCreating (DbContext)
modelBuilder.Filter("CustomerActive", (Customers c) => c.IsActive);

这就是全部!现在到处都在那里客户的质疑,无论是直接通过导航属性或包含 s时,predicate将被添加到查询中。

That's all! Now everywhere where Customers are queried, be it directly, through navigation properties or in Includes, the predicate will be added to the query.

你希望所有的客户?你可以简单地做关闭过滤器每上下文实例

Do you want all customers? You can simply turn the filter off per context instance by doing

context.DisableFilter("CustomerActive");

有只有一个小故障(或警告),我发现至今。如果有两个实体,儿童并没有对不返回任何记录,那么此查询...

There's only one glitch (or caveat) I discovered so far. If there are two entities, Parent and Child and there is a filter on Parent that doesn't return any records, then this query ...

context.Children.Include(c => c.Parent)

...不返回任何东西。但是,从查询的形状,我希望它返回儿童实体空的父母。

这是因为在SQL有一个 INNER JOIN 之间的孩子计算结果为假一predicate 。一个 OUTER JOIN 将给予预期的行为,但当然,我们不能从这个库需求将的的智能。

This is because in SQL there is an INNER JOIN between Parent and Child and a predicate on Parent that evaluates to false. An OUTER JOIN would give the expected behavior, but of course we can't demand from this library to be that smart.

这篇关于网页API $带过滤器扩展的IQueryable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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