How to select property from expanded property? Rec

2019-09-10 09:40发布

I have a class:

Person which has property public virtual ICollection<Call> ReceivedCalls { get; set; }

Then the Call is :

 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; }
    }

I have a PersonController and CallsController (both are scaffolded WEB API OData controllers).

I can fetch people and their calls (and who called) by the link:

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

and I get pretty output(no worry, fake data):

{
  "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"
        }

But know I would like to know what is an Email of the caller,

I tried that:

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

but I get an error:

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>

Question What link should I use to access the Caller for every person?

EDIT

It gave me:

{
  "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
     },{

which is Email of a Person not of a Caller which is ApplicationUser

1条回答
女痞
2楼-- · 2019-09-10 10:11

This should work

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

I have tried it on the TripPin example site with this url

http://services.odata.org/V4/(S(0yzzniu3ezyzy1ragcduis3n))/TripPinServiceRW/People?$expand=Friends($expand=Trips($select=TripId))
查看更多
登录 后发表回答