如何构建OData GET请求仅获取指定的三个字段
Got it, let's sort out that OData request so you only get the three fields you care about! The key here is that you can nest $select inside $expand to specify exactly which fields you want from the related Franchise entity, instead of pulling the entire object.
Correct OData Request
Use this request to fetch just PostalCode, FranchiseId, and the nested FranchiseeName:
#url#&$select=PostalCode,FranchiseId&$expand=Franchise($select=FranchiseeName)
What This Does
$select=PostalCode,FranchiseId: Tells the API to only return these two top-level fields.$expand=Franchise($select=FranchiseeName): Expands theFranchiserelation, but restricts it to only include theFranchiseeNamefield (no extra data likeCreatedDateTimeorCity).
Expected Response Structure
With this request, the API will return a structure like this (note the nested Franchise object with only your desired field):
{ "PostalCode": "48103", "FranchiseId": 397, "Franchise": { "FranchiseeName": "Appliance Inc - Ann Arbor, MI - MLY 1001" } }
Getting FranchiseeName at the Top Level (If Supported)
If you want FranchiseeName to be a top-level field (matching your exact desired output), some OData 4.0 services support the $apply operator to project nested fields up. Try this request if your API allows it:
#url#&$apply=compute(FranchiseeName as Franchise/FranchiseeName)&$select=PostalCode,FranchiseId,FranchiseeName
If your API doesn't support $apply, you'll need to do a quick transformation on the client side to extract Franchise.FranchiseeName and move it to the top level of the object.
内容的提问来源于stack exchange,提问作者DoArNa

