Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

5. Select and Expand to JSON

IharYakimush edited this page Mar 1, 2018 · 1 revision

Usings

    using System;
    using System.Linq;

    using Community.OData.Linq;
    using Community.OData.Linq.Json;

    using Newtonsoft.Json;

Select only Name, expand Id from RelatedEntity and top 1 from RelatedEntitiesCollection where Id greater than 200

Code

    IQueryable<Sample> dataSet = Sample.CreateQuerable();
    var result = dataSet.OData().SelectExpandJsonToken(
        "Name",
        "RelatedEntity($select=Id),RelatedEntitiesCollection($filter=Id ge 200;$top=1)");

    Console.WriteLine(result.ToString(Formatting.Indented));

Console output

[
  {
    "Name": "name1",
    "RelatedEntitiesCollection": [],
    "RelatedEntity": {
      "Id": 10
    }
  },
  {
    "Name": "name2",
    "RelatedEntitiesCollection": [
      {
        "Id": 200,
        "Name": "name200"
      }
    ],
    "RelatedEntity": {
      "Id": 20
    }
  },
  {
    "Name": "name3",
    "RelatedEntitiesCollection": [
      {
        "Id": 300,
        "Name": "name300"
      }
    ],
    "RelatedEntity": {
      "Id": 30
    }
  }
]
Clone this wiki locally