Quantcast
Viewing all articles
Browse latest Browse all 7205

ODataContext - How to filter data by a foreign key value


Sometimes it is required to filter some entities by a foreign key value.

This example demonstrates how to select all products that are associated with a required category. The Product entity has the Category navigation property. To filter products by the category ID, we need to expand the Category property using the ODataStore.expand option. In this case, we will be able to access a key field of a navigation property. This example operates with the Northwind online OData service. We can determine that Category is a navigation property of the Product one by typing the following URL in our browser "http://services.odata.org/Northwind/Northwind.svc/$metadata". See a screenshot below.


Image may be NSFW.
Clik here to view.

In this case our code should be as follows:

[JScript]
TestExpand.db = new DevExpress.data.ODataContext({ url: "http://services.odata.org/Northwind/Northwind.svc", errorHandler: function(error){ alert(error.message);}, entities: { Products: { key: "ProductID"}}});var viewModel = { dataSource: TestExpand.db.Products.toDataSource({ select: ['ProductID', 'ProductName', 'Category.CategoryID', 'Category.CategoryName'], expand: ['Category'], filter: ['Category.CategoryID', '=', 5]})};


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>