实现与$和的WebAPI选择ODataQueryOptions [英] Implementing $select with WebApi and ODataQueryOptions

查看:382
本文介绍了实现与$和的WebAPI选择ODataQueryOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一些OData的functionaility使用ODataQueryOptions自定义DAL。

I'm trying to implement some OData functionaility with a custom DAL using ODataQueryOptions.

我的DAL使用设计时生成的类型化数据表。通过截取ODataQueryOptions的SelectExpand财产我可以得到我们的DAL只负载所需的列。

My DAL uses design time generated typed data tables. By intercepting the SelectExpand property of ODataQueryOptions i can get our DAL to only load the columns required.

我怎么那么仅仅需要将数据返回。

How do i then return just the data required.

我目前从我们的类型数据表中的数据引爆成ListOf某些类型的数据转院对象,但最终与大量从不需要的列空数据。

I am currently tipping the data from our type datatables into a ListOf some typed data tranfer objects but then end up with lots of null data from the columns which aren't required.

我觉得我应该可以做一些LINQ查询选择刚刚我需要直接使用DTO的类型完全的类型化DataTable绕过列。这可能吗?

I feel like i should be able do some LINQ query to select the just the columns I need straight from the typed datatable bypassing using typed DTOs altogether. Is this possible?

推荐答案

您需要做同样的事情,<一个href=\"https://aspnetwebstack.$c$cplex.com/SourceControl/latest#src/System.Web.Http.OData/OData/Query/SelectExpandQueryOption.cs\"相对=nofollow> SelectExpandQueryOption.ApplyTo 一样。

You need to do the same thing that SelectExpandQueryOption.ApplyTo does.

1)优化查询到后端。相反,从数据库中获取了整个实体,只得到客户要求的性能和包装是导致IEdmEntityObject。返回集合作为EdmEntityObjectCollection。这个步骤是可选的。你可以选择忽略此步,返回IQueryable的,仍然可以得到$选择的工作。

1) Optimize the query to the backend. Instead of getting the whole entity from the database, get only the properties the client asked for and wrap that result in an IEdmEntityObject. Return the collection as EdmEntityObjectCollection. This step is optional. You could choose to ignore this step and return IQueryable and still get $select to work.

2)告诉OData的格式化仅序列化所要求的领域。这可以通过使用扩展方法 Request.SetSelectExpandClause 馅SelectExpandClause Request对象上进行。

2) Tell the OData formatter to serialize only the requested fields. This can be done by stuffing the SelectExpandClause on the Request object using the extension method Request.SetSelectExpandClause.

public class CustomersController : ODataController
{
    public IEnumerable<Customer> Get(ODataQueryOptions<Customer> query)
    {
        Customer[] customers = new[] { new Customer { ID = 42, Name = "Raghu" } };

        // Apply query
        var result = customers;

        // set the SelectExpandClause on the request to hint the odata formatter to 
        // select/expand only the fields mentioned in the SelectExpandClause.
        if (query.SelectExpand != null)
        {
            Request.SetSelectExpandClause(query.SelectExpand.SelectExpandClause);
        }

        return result;
    }
}

这篇关于实现与$和的WebAPI选择ODataQueryOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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