简体   繁体   English

OData V3排除属性

[英]OData V3 exclude property

I wish to exclude a particular property from a entity collection over odata. 我希望从odata的实体集合中排除特定属性。

I've used .Expand("Files") to retrieve that particular collection, they are files in the database and I want all of the metadata (like Name and MimeType) of the file, but not the binary body itself. 我已经使用.Expand(“ Files”)来检索该特定集合,它们是数据库中的文件,我想要文件的所有元数据(例如Name和MimeType),而不是二进制主体本身。

I am not allowed to change the OData service itself so if it's possible at all, it must be done using a instruction in the odata query. 我不允许更改OData服务本身,因此,如果有可能,必须使用odata查询中的指令来完成。

Any thoughts? 有什么想法吗? Thx in advance. 提前谢谢。

UPDATE2: Vagif has been helpful, but made me realize I did not phrase my question entirely correctly. UPDATE2:Vagif很有帮助,但让我意识到我没有完全正确地表达我的问题。 Once again: apologies. 再次:道歉。 The actual property can be in the class that is returned by expanding "Files", but it must be null. 实际属性可以在通过扩展“文件”返回的类中,但是它必须为null。 In other words: I'd like the expanded child records to have a property not being filled with data. 换句话说:我希望扩展的子记录具有不填充数据的属性。

UPDATE: Thx nemesv. 更新:Thx nemesv。 I should indeed have been more specific. 我确实应该更具体一些。 The odata service is build using the odata nuget package using visual studio using c#. odata服务是使用odata nuget软件包和Visual Studio(使用c#)构建的。 The client uses the same tools. 客户端使用相同的工具。 The server however uses odata 5.0.1. 但是,服务器使用odata 5.0.1。 The client any version I want, (5.6 now I think). 客户端我想要的任何版本,(我认为现在是5.6)。

If you use C# and LINQ on the client side, you can select the properties you want to include in the payload using Select clause and anonymous types, eg: 如果在客户端使用C#和LINQ,则可以使用Select子句和匿名类型来选择要包含在有效负载中的属性,例如:

var results = context.MyCollection.Select(x => new { x.Name, x.Category }); var结果= context.MyCollection.Select(x => new {x.Name,x.Category});

UPDATE 更新

The trick with selecting columns from expanded entity is that you don't need to explicitly expand it: WCF Data Services LINQ provider fill figure it out. 从扩展的实体中选择列的技巧是,您无需显式地扩展它:WCF数据服务LINQ提供程序可以解决这个问题。 Here's an example using Northwind data model: 这是使用Northwind数据模型的示例:

[Test]
public void SelectProductAndCategoryColumns()
{
    var result = _context.Products
        .Where(x => x.ProductID == 1)
        .Select(x => new { x.ProductID, x.Category.CategoryID, x.Category.CategoryName })
        .Single();
    Assert.AreEqual(1, result.ProductID);
    Assert.AreNotEqual(0, result.CategoryID);
    Assert.IsNotNull(0, result.CategoryName);
}

Note that I am chaining expanded columns in the Select clause without using Expand clause. 请注意,我在Select子句中链接了扩展列,而没有使用Expand子句。 And it works this way (but only for one level, you can't chain them further). 它以这种方式工作(但仅适用于一个级别,您无法进一步链接它们)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM