本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/using-select-expand-and-value

by Mike Wasson+

Web API 2 adds support for the $expand, $select, and $value options in OData. These options allow a client to control the representation that it gets back from the server.+

    • $expand causes related entities to be included inline in the response.
    • $select selects a subset of properties to include in the response.
    • $value gets the raw value of a property.

+

Example Schema

For this article, I'll use an OData service that defines three entities: Product, Supplier, and Category. Each product has one category and one supplier.+

+

Here are the C# classes that define the entity models:+

C#Copy
public class Supplier
{
[Key]
public string Key {get; set; }
public string Name { get; set; }
}
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
} public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; } [ForeignKey("Category")]
public int CategoryId { get; set; }
public Category Category { get; set; } [ForeignKey("Supplier")]
public string SupplierId { get; set; }
public virtual Supplier Supplier { get; set; }
}

Notice that the Product class defines navigation properties for the Supplier and Category. The Category class defines a navigation property for the products in each category.+

To create an OData endpoint for this schema, use the Visual Studio 2013 scaffolding, as described in Creating an OData Endpoint in ASP.NET Web API. Add separate controllers for Product, Category, and Supplier.+

Enabling $expand and $select

In Visual Studio 2013, the Web API OData scaffolding creates a controller that automatically supports $expand and $select. For reference, here are the requirements to support $expand and $select in a controller.+

For collections, the controller's Get method must return an IQueryable.+

C#Copy
[Queryable]
public IQueryable<Category> GetCategories()
{
return db.Categories;
}

For single entities, return a SingleResult<T>, where T is an IQueryable that contains zero or one entities.+

C#Copy
[Queryable]
public SingleResult<Category> GetCategory([FromODataUri] int key)
{
return SingleResult.Create(db.Categories.Where(c => c.ID == key));
}

Also, decorate your Get methods with the [Queryable] attribute, as shown in the previous code snippets. Alternatively, call EnableQuerySupport on the HttpConfiguration object at startup. (For more information, see Enabling OData Query Options.)+

Using $expand

When you query an OData entity or collection, the default response does not include related entities. For example, here is the default response for the Categories entity set:+

consoleCopy
{
"odata.metadata":"http://localhost/odata/$metadata#Categories",
"value":[
{"ID":1,"Name":"Apparel"},
{"ID":2,"Name":"Toys"}
]
}

As you can see, the response does not include any products, even though the Category entity has a Products navigation link. However, the client can use $expand to get the list of products for each category. The $expand option goes in the query string of the request:+

consoleCopy
GET http://localhost/odata/Categories?$expand=Products

Now the server will include the products for each category, inline with the categories. Here is the response payload:+

consoleCopy
{
"odata.metadata":"http://localhost/odata/$metadata#Categories",
"value":[
{
"Products":[
{"ID":1,"Name":"Hat","Price":"15.00","CategoryId":1,"SupplierId":"CTSO"},
{"ID":2,"Name":"Scarf","Price":"12.00","CategoryId":1,"SupplierId":"CTSO"},
{"ID":3,"Name":"Socks","Price":"5.00","CategoryId":1,"SupplierId":"FBRK"}
],
"ID":1,
"Name":"Apparel"
},
{
"Products":[
{"ID":4,"Name":"Yo-yo","Price":"4.95","CategoryId":2,"SupplierId":"WING"},
{"ID":5,"Name":"Puzzle","Price":"8.00","CategoryId":2,"SupplierId":"WING"}
],
"ID":2,
"Name":"Toys"
}
]
}

Notice that each entry in the "value" array contains a Products list.+

The $expand option takes a comma-separated list of navigation properties to expand. The following request expands both the category and the supplier for a product.+

consoleCopy
GET http://localhost/odata/Products(1)?$expand=Category,Supplier

Here is the response body:+

consoleCopy
{
"odata.metadata":"http://localhost/odata/$metadata#Products/@Element",
"Category": {"ID":1,"Name":"Apparel"},
"Supplier":{"Key":"CTSO","Name":"Contoso, Ltd."},
"ID":1,
"Name":"Hat",
"Price":"15.00",
"CategoryId":1,
"SupplierId":"CTSO"
}

You can expand more than one level of navigation property. The following example includes all the products for a category and also the supplier for each product.+

consoleCopy
GET http://localhost/odata/Categories(1)?$expand=Products/Supplier

Here is the response body:+

consoleCopy
{
"odata.metadata":"http://localhost/odata/$metadata#Categories/@Element",
"Products":[
{
"Supplier":{"Key":"CTSO","Name":"Contoso, Ltd."},
"ID":1,"Name":"Hat","Price":"15.00","CategoryId":1,"SupplierId":"CTSO"
},
{
"Supplier":{"Key":"CTSO","Name":"Contoso, Ltd."},
"ID":2,"Name":"Scarf","Price":"12.00","CategoryId":1,"SupplierId":"CTSO"
},{
"Supplier":{
"Key":"FBRK","Name":"Fabrikam, Inc."
},"ID":3,"Name":"Socks","Price":"5.00","CategoryId":1,"SupplierId":"FBRK"
}
],"ID":1,"Name":"Apparel"
}

By default, Web API limits the maximum expansion depth to 2. That prevents the client from sending complex requests like $expand=Orders/OrderDetails/Product/Supplier/Region, which might be inefficient to query and create large responses. To override the default, set the MaxExpansionDepth property on the [Queryable] attribute.+

C#Copy
[Queryable(MaxExpansionDepth=4)]
public IQueryable<Category> GetCategories()
{
return db.Categories;
}

For more information about the $expand option, see Expand System Query Option ($expand) in the official OData documentation.+

Using $select

The $select option specifies a subset of properties to include in the response body. For example, to get only the name and price of each product, use the following query:+

consoleCopy
GET http://localhost/odata/Products?$select=Price,Name

Here is the response body:+

consoleCopy
{
"odata.metadata":"http://localhost/odata/$metadata#Products&$select=Price,Name",
"value":[
{"Price":"15.00","Name":"Hat"},
{"Price":"12.00","Name":"Scarf"},
{"Price":"5.00","Name":"Socks"},
{"Price":"4.95","Name":"Yo-yo"},
{"Price":"8.00","Name":"Puzzle"}
]
}

You can combine $select and $expand in the same query. Make sure to include the expanded property in the $select option. For example, the following request gets the product name and supplier.+

consoleCopy
GET http://localhost/odata/Products?$select=Name,Supplier&$expand=Supplier

Here is the response body:+

consoleCopy
{
"odata.metadata":"http://localhost/odata/$metadata#Products&$select=Name,Supplier",
"value":[
{
"Supplier":{"Key":"CTSO","Name":"Contoso, Ltd."},
"Name":"Hat"
},
{
"Supplier":{"Key":"CTSO","Name":"Contoso, Ltd."},
"Name":"Scarf"
},
{
"Supplier":{"Key":"FBRK","Name":"Fabrikam, Inc."},
"Name":"Socks"
},
{
"Supplier":{"Key":"WING","Name":"Wingtip Toys"},
"Name":"Yo-yo"
},
{
"Supplier":{"Key":"WING","Name":"Wingtip Toys"},
"Name":"Puzzle"
}
]
}

You can also select the properties within an expanded property. The following request expands Products and selects category name plus product name.+

consoleCopy
GET http://localhost/odata/Categories?$expand=Products&$select=Name,Products/Name

Here is the response body:+

consoleCopy
{
"odata.metadata":"http://localhost/odata/$metadata#Categories&$select=Name,Products/Name",
"value":[
{
"Products":[ {"Name":"Hat"},{"Name":"Scarf"},{"Name":"Socks"} ],
"Name":"Apparel"
},
{
"Products":[ {"Name":"Yo-yo"},{"Name":"Puzzle"} ],
"Name":"Toys"
}
]
}

For more information about the $select option, see Select System Query Option ($select) in the official OData documentation.+

Getting Individual Properties of an Entity ($value)

There are two ways for an OData client to get an individual property from an entity. The client can either get the value in OData format, or get the raw value of the property.+

The following request gets a property in OData format.+

consoleCopy
GET http://localhost/odata/Products(1)/Name

Here is an example response in JSON format:+

consoleCopy
HTTP/1.1 200 OK
Content-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8
DataServiceVersion: 3.0
Content-Length: 90 {
"odata.metadata":"http://localhost:14239/odata/$metadata#Edm.String",
"value":"Hat"
}

To get the raw value of the property, append $value to the URI:+

consoleCopy
GET http://localhost/odata/Products(1)/Name/$value

Here is the response. Notice that the content type is "text/plain", not JSON.+

consoleCopy
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
DataServiceVersion: 3.0
Content-Length: 3 Hat

To support these queries in your OData controller, add a method named GetProperty, where Property is the name of the property. For example, the method to get the Name property would be named GetName. The method should return the value of that property:+

C#Copy
public async Task<IHttpActionResult> GetName(int key)
{
Product product = await db.Products.FindAsync(key);
if (product == null)
{
return NotFound();
}
return Ok(product.Name);
}

[转]Using $select, $expand, and $value in ASP.NET Web API 2 OData的更多相关文章

  1. [转]ASP.NET web API 2 OData enhancements

    本文转自:https://www.pluralsight.com/blog/tutorials/asp-net-web-api-2-odata-enhancements Along with the ...

  2. ASP.NET Web API基于OData的增删改查,以及处理实体间关系

    本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先是比较典型的一对多关系,Supplier和Product. public class Product { ...

  3. [转]ASP.NET Web API基于OData的增删改查,以及处理实体间关系

    本文转自:http://www.cnblogs.com/darrenji/p/4926334.html 本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先 ...

  4. [转]ASP.NET Web API对OData的支持

    http://www.cnblogs.com/shanyou/archive/2013/06/11/3131583.html 在SOA的世界中,最重要的一个概念就是契约(contract).在云计算的 ...

  5. ASP.NET Web API 2 OData v4教程

    程序数据库格式标准化的开源数据协议 为了增强各种网页应用程序之间的数据兼容性,微软公司启动了一项旨在推广网页程序数据库格式标准化的开源数据协议(OData)计划,于此同时,他们还发 布了一款适用于OD ...

  6. [转]Supporting OData Query Options in ASP.NET Web API 2

    本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/suppor ...

  7. ASP.NET Web API系列教程目录

    ASP.NET Web API系列教程目录 Introduction:What's This New Web API?引子:新的Web API是什么? Chapter 1: Getting Start ...

  8. Asp.Net Web Api 与 Andriod 接口对接开发经验,给小伙伴分享一下!

    最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用Asp.Net Web Api去跟 Andriod 那端做接口对接工作,自己也是第一次接触Web ...

  9. Asp.Net Web Api 与 Andriod 接口对接开发

    Asp.Net Web Api 与 Andriod 接口对接开发经验,给小伙伴分享一下!   最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用A ...

随机推荐

  1. webservice 创建及调用

    1.创建一个空白项目 2.在此项目上新建项--添加一个web服务 (.asmx) 这样就创建好了一个webservice --------------------------------------- ...

  2. 单机版solr7.3

    搭建Solr7.3单机版 原博客  https://blog.csdn.net/yiqing_summer/article/details/79934584 1.Solr下载:http://mirro ...

  3. 【sonar-block】Use try-with-resources or close this "BufferedInputStream" in a "finally" clause.

    自己的理解: try-with-resources是JDK7的新语法结构,主要功能是自动关闭资源而不需要在finally里面手动close()关闭, 而且最重要的是,try块中的异常不会被close( ...

  4. luoguP2479 [SDOI2010]捉迷藏

    https://www.luogu.org/problemnew/show/P2479 据说可以用线段树做但是我不会,只能写一个 KD-Tree 了 对于每个点求出距离它最远的点和最近的点的距离,然后 ...

  5. BZOJ3510 首都

    题目描述 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从这个星球消失, ...

  6. [Swift]队列Queue的两种版本:(1)用类包装Queue (2)用泛型包装Queue

    队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进行删除操作的 ...

  7. Spark JavaRDD、JavaPairRDD、Dataset相互转换与打印

    主要内容: 1. List转JavaRDD,打印JavaRDD 2. List转JavaRDD,JavaRDD转JavaPairRDD,打印JavaPairRDD 3. JavaRDD<Stri ...

  8. flink学习笔记:DataSream API

    本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...

  9. 在Mondrian Virtual OLAP Cube中观察星座模型多事实表度量值的聚合

    这样设置的Schema文件会怎么样呢?用Saiku预览一下. 如果这时候想同时引用两个项目进行计算就会出问题了.那么这种情况怎么解决? 参考网上一段实现思路 <VirtualCube name= ...

  10. Intellij IDEA 封装Jar包(提示错误: 找不到或无法加载主类)

    封装的过程如下: 然后准备打包 选择Build或者ReBuild即可. 但这样就会引起开始第一个图的问题.提示无法加载主类,另外一个情况就是所有的外部第三方jar包都被封装到一个jar包里面了. 那么 ...