Anatolii Humennyi
Anatolii Humennyi

Reputation: 1856

Query options for getting single value with the specified key in ASP.NET OData Controller

When I created OData controller with default "demo" implementation I noticed that there is query options in the method which gets a specific value with a specific Id:

    // GET: odata/TestModels(5)
    public IHttpActionResult GetTestModel([FromODataUri] int key, ODataQueryOptions<TestModel> queryOptions)
    {
        // validate the query.
        try
        {
            queryOptions.Validate(_validationSettings);
        }
        catch (ODataException ex)
        {
            return BadRequest(ex.Message);
        }

        //return Ok<TestModel>(testModel);
        return StatusCode(HttpStatusCode.NotImplemented);
    }

I can't find information how to use query options for getting an individual value/ For getting a list of values we can use something like:

        var results = queryOptions.ApplyTo(_testModelsRepository.TestModels.AsQueryable());

But how to use them for returning a specific value if this method returns specific TestModel but not IQueryable<TestModel>? And does it make sense to use query options for getting an individual value? Because I didn't find any example on the Internet with using query options for getting a specific value. And if it doesn't make sense then why Visual Studio adds query options to the method for getting a specific value?

Upvotes: 0

Views: 1673

Answers (1)

Fan Ouyang
Fan Ouyang

Reputation: 2132

In your case, you can just use [Queryable] attribute in your method, and remove the ODataQueryOptions in parameter, OData will apply this for your result.

Or use SingleResult:

SingleResult result = SingleResult.Create(TestModels.AsQueryable());

Upvotes: 1

Related Questions