Search code examples
.netlinqasp.net-coreodata.net-6.0

is there a way to query OData with a linq query


I'm using [EnableQuery] and returning an IQueryable from my api controller like this:

[HttpGet]
[EnableQuery]
public IQueryable<Category> Get()
{
    return getCategories().AsQueryable();
}

Is it possible to consume this odata url using linq, e.g. something like:

var res = await getOdata("Category").Where(item => item.Size > 100).ToArrayAsync();

does something like this exist ? I could only find examples of odata api's being built and consumed via string parameters but no linq.


Solution

  • There are mainly 2 ways to add Query Options to a DataServiceQuery

    1. Using AddQueryOption method.
    2. Using strongly typed C# LINQ query methods.

    So you can query OData with LINQ query. For more details, you can check below official doc.

    LINQ Query Methods