如何从扩展财产中选择房产? ReceivedCalls / Caller?$ select = Email'在$ select或$ expand表达式中无效

时间:2015-06-20 12:13:35

标签: .net odata asp.net-web-api2

我有一个班级:

Person,其属性为public virtual ICollection<Call> ReceivedCalls { get; set; }

然后Call是:

 public class Call {
        public int Id { get; set; }
        public virtual Person Callee { get; set; }
        public virtual ApplicationUser Caller { get; set; }
        public DateTime TimeStamp { get; set; }
    }

我有PersonControllerCallsController(两者都是脚手架的WEB API OData控制器)。

我可以通过链接获取人员和他们的电话(以及谁打电话):

http://localhost:17697/odata/Person?$expand=ReceivedCalls/Caller

我得到了相当的输出(不用担心,假数据):

{
  "odata.metadata":"http://localhost:17697/odata/$metadata#Person","value":[
    {
      "ReceivedCalls":[

      ],"Id":1,"FirstName":"John","LastName":"Doe","CellNumber":"123-456-789","SecondaryPhoneNumber":"98873213","Email":null,"Address":"1street 2","BirthDate":"2014-10-02T00:00:00","Pesel":"312312312","Notes":"Lorem ipsum note","StatusId":null,"PersonalDataProcessing":false,"IsCalledRightNow":false,"CustomerStatusId":null,"NIP":null
    },{
      "ReceivedCalls":[
        {
          "Caller":{
            "DisplayAnnouncmentNotification":true,"Email":"s8359@pjwstk.edu.pl","EmailConfirmed":false,"PasswordHash":"ABk0SI5boLt0YWE36G9PHGBMXEecl9eN3MQNHIlYy7nP8pHvr/IVsDVQ7oCtRuz/Uw==","SecurityStamp":"615f8ad3-ca19-42b9-9686-51597eef578c","PhoneNumber":null,"PhoneNumberConfirmed":false,"TwoFactorEnabled":false,"LockoutEndDateUtc":null,"LockoutEnabled":false,"AccessFailedCount":0,"Id":"812f907d-7079-433f-9503-b20e1d5a9ef7","UserName":"s8359@pjwstk.edu.pl"
          },"Id":4,"TimeStamp":"2014-11-27T13:02:42.91"
        }

但我知道我想知道调用者的Email是什么,

我试过了:

http://localhost:17697/odata/Person?$expand=ReceivedCalls/Caller?$select=Email

但是我收到了错误:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code/>
<m:message xml:lang="en-US">
The query specified in the URI is not valid. Term 'ReceivedCalls/Caller?$select=Email' is not valid in a $select or $expand expression.
</m:message>
<m:innererror>
<m:message>
Term 'ReceivedCalls/Caller?$select=Email' is not valid in a $select or $expand expression.
</m:message>
<m:type>Microsoft.Data.OData.ODataException</m:type>
<m:stacktrace>
w Microsoft.Data.OData.Query.NonOptionSelectExpandTermParser.BuildExpandTermToken(Boolean isInnerTerm, PathSegmentToken pathToken)
 w Microsoft.Data.OData.Query.SelectExpandTermParser.ParseExpand()
 w Microsoft.Data.OData.Query.ODataUriParser.ParseSelectAndExpandImplementation(String select, String expand, IEdmEntityType elementType, IEdmEntitySet entitySet)
 w System.Web.Http.OData.Query.SelectExpandQueryOption.get_SelectExpandClause()
 w System.Web.Http.OData.Query.Validators.SelectExpandQueryValidator.Validate(SelectExpandQueryOption selectExpandQueryOption, ODataValidationSettings validationSettings)
 w System.Web.Http.OData.Query.SelectExpandQueryOption.Validate(ODataValidationSettings validationSettings)
 w System.Web.Http.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)
 w System.Web.Http.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)
 w System.Web.Http.QueryableAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)
 w System.Web.Http.QueryableAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)
 w System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
</m:stacktrace>
</m:innererror>
</m:error>

问题我应该使用哪个链接来访问每个人的Caller

修改

它给了我:

{
  "odata.metadata":"http://localhost:17697/odata/$metadata#Person&$select=Email","value":[
    {
      "Email":null
    },{
      "Email":null
    further DOWN there is:
     },{
     "Email":"a@a.pl" // a@a.pl is email of one of the Person
     },{

Email Person Caller而不是ApplicationUser rep(sample(1:5), sample(1:5))

1 个答案:

答案 0 :(得分:0)

这应该有效

http://localhost:17697/odata/Person?$expand=ReceivedCalls($expand=Caller($select=Email))

我在TripPin示例网站上尝试了这个网址

http://services.odata.org/V4/(S(0yzzniu3ezyzy1ragcduis3n))/TripPinServiceRW/People?$expand=Friends($expand=Trips($select=TripId))