Is there an ODATA query for linq where the expression (ODATA - Linq)

Basically,

I would like to convert the odata query expression "$ filter", "itemID eq 1" to where (w => w.itemID == 1)

Is there a library ready for this operation? Otherwise, I need to encode it using the DynamicLinq and linqKit classes.

+6
source share
2 answers

I use Microsoft WebAPI with the following NuGet packages installed:

http://nuget.org/packages/Microsoft.Data.OData/

http://nuget.org/packages/microsoft.aspnet.webapi.odata

Which allows me to write things like:

using System.Web.Http; using System.Web.Http.OData.Query; // Some stuff left out [Queryable] public IQueryable<Item> Get(ODataQueryOptions<Item> query) { var items = query.ApplyTo(from item in context.Items select item); return (IQueryable<Item>) items; } 

Then I can call it with jQuery Ajax (for example, I prefer using BackboneJS):

 $.ajax({ url: '/api/items', data: $.param({ '$filter': 'ID eq 1' }) }); 

which will then only return elements with an identifier equal to 1, which I think is what you are after?

If ItemID is the main identifier of the object you are retrieving, I will probably follow REST principles and create an API, where the URL to get the item with identifier 1 will look like this:

 /api/items/1 

And use only oData queries in the collection of elements if I requested based on other properties of the elements of the collection, or do something like below, for example, when extracting the top 10 records.

 $.ajax({ url: '/api/items', data: $.param({ '$top': 10 }) }); 
+9
source

You can use the following NuGet package to apply the filter: https://www.nuget.org/packages/Community.OData.Linq

Code example:

 using System.Linq; using Community.OData.Linq; // dataSet in this example - any IQuerable<T> Sample[] filterResult = dataSet.OData().Filter("Id eq 2 or Name eq 'name3'").ToArray(); 

Currently supported: filter and order in v4 format

+1
source

All Articles