展开一个投影(选择)的WCF数据服务(OData的) [英] Expand in a Projection (Select) for WCF Data Services (OData)

查看:146
本文介绍了展开一个投影(选择)的WCF数据服务(OData的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个OData的查询,看起来像这样(我的实际查询要复杂得多):

Say I have an OData query that looks like this (My actual query is much more complex):

Orders.Select(z => new { z.SubOrder.Addresses,
                         z.SubOrder.Cost,
                         z.SubOrder.SubOrderId, 
                         z.Sequence});

这工作得很好。除地址对象上有(StateRef)的子对象。由于StateRef确实对国家表查询,它返回为空。

This works fine. Except that the Address object has a sub object on it (StateRef). Since StateRef does a look-up on the State table, it is returned as null.

要说明这一点,这里是如何的地址对象地址可能如下的例子:

To illustrate, here is an example of how the address object Address might look:

Address:
    string         Street 1
    string         Street 2
    StateRef       PrimaryState
    string         City
    // ... 42 other string attributes not shown ...

StateRef 对象上有国家的名字,但也有一些其他重要的国家性质(也许州鸟?)

The StateRef object has the name of the state on it, but also has some other important State properties (maybe state bird?)

所以,我想知道是什么,我必须现在z.SubOrder.Addresses创建一个子投影包含所有46属性只是这样我就可以访问 PrimaryState 项目? (我希望不是

So, what I am wondering is, do I have to now create a "sub projection" for z.SubOrder.Addresses that contains all 46 attributes just so that I can access the PrimaryState item? (I Hope NOT)

除了是方式更多的代码,这也意味着我必须使用匿名类型。这让我的映射必须由专人(而不是使用AutoMapper)。

Aside from being way more coding, it also means I have to use anonymous types. Which makes my mapping have to be by hand (instead of using AutoMapper).

所以我在寻找一种方法来扩大的StateRef投影里面?

事情是这样的:

Orders.Select(z => new { z.SubOrder.Addresses.Expand("PrimaryState"),
                         z.SubOrder.Cost,        ^
                         z.SubOrder.SubOrderId,  |
                         z.Sequence});           |
                                                 |
// This is not allowed by the compiler ----------+

尝试这种给这个错误:

Trying this give this error:

无效的匿名类型成员声明。匿名类型的成员必须有一个成员赋值,简单名称或成员访问来声明。

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

更新: 下面是一个例子查询来说明什么,我问:

Update: Here is an example query to illustrate what I am asking about:

Users.Take(10).Select(x=>new { x.Id, x.Reputation, x.Comments})

运行,对<一href="http://data.stackexchange.com/stackoverflow/atom">data.stackexchange.com/stackoverflow/atom".你会看到评论有一个邮政对象返回null。

Run that against "data.stackexchange.com/stackoverflow/atom". You will see that Comments has a Post object that returns null.

我需要回到它里面的值。

I need that to return the values inside of it.

注:我知道我可以手动输入所有的人都出到了亚健康的投影。看了上面为什么我不希望这样。

推荐答案

这当然是有可能做到这一点。对于概念验证尝试执行这样的:

It is certainly possible to do that. For a proof of concept try executing this:

var uri = new Uri( "http://data.stackexchange.com/stackoverflow/atom/Users()?$top=10&$expand=Comments/Post&$select=Id,Reputation,Comments/" );
entities.Execute<User>( uri, "GET", false ).Select( x => new { x.Id, x.Reputation, x.Comments } );

扩大的正确用法是这样的:

The correct usage of expand is like this:

entities.Users.Expand( "Comments/Post" ).Take( 10 ).ToArray();

我不知道为什么该库的作家们决定采用扩大与预测,以禁止,但作为概念显示了上述证据,这当然是可以这样做。

I don't know why the writers of the library have decided to disallow using expand with projections, but as the above proof of concept shows, it is certainly possible to do so.

如果你不介意接收整个用户,使投影后,你可以用第二个例子。否则,你可以编写自己的助手,这将产生的URI的第一个例子,执行它们,之后添加投影。

If you don't mind receiving the entire user and making the projection after that, you can go with the second example. Else you can write your own helpers which will produce the URI from the first example, execute them, and add the projection after that.

这篇关于展开一个投影(选择)的WCF数据服务(OData的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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