Search code examples
c#linqodata

OData V3 exclude property


I wish to exclude a particular property from a entity collection over 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.

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.

Any thoughts? Thx in advance.

UPDATE2: Vagif has been helpful, but made me realize I did not phrase my question entirely correctly. Once again: apologies. The actual property can be in the class that is returned by expanding "Files", but it must be null. In other words: I'd like the expanded child records to have a property not being filled with data.

UPDATE: Thx nemesv. I should indeed have been more specific. The odata service is build using the odata nuget package using visual studio using c#. The client uses the same tools. The server however uses odata 5.0.1. The client any version I want, (5.6 now I think).


Solution

  • 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, e.g.:

    var results = 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. Here's an example using Northwind data model:

    [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. And it works this way (but only for one level, you can't chain them further).