OData
-------------------------------------------------------------------------------------
Demonstrates how to use OData data sources in Windows Store applications.

This sample uses the NorthWind OData service available at http://services.odata.org/Northwind/Northwind.svc/. 
When you select a category on the top grid, the corresponding products are shown on the bottom grid.

To use OData sources, you should start by adding a service reference to your project, then
specifying the URL of the OData provider. In our case, the URL is the one mentioned above, and the
service was added directly to the Silverlight client.

Once the service reference has been added to the project, you can obtain the data by instantiating
a data context, creating one DataServiceCollection for each entity collection you need in your application,
and finally calling the DataServiceCollection.LoadAsync method.

For example, the code below populates the Categories grid in the sample:

<code>
	// create context
    _context = new NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc/"));

    // bind grid to categories collection
    var categories = new DataServiceCollection<Category>();
    _flexCategories.ItemsSource = categories;

	// load categories
    categories.LoadCompleted += (s, ee) =>
    {
		if (categories.Continuation != null)
			categories.LoadNextPartialSetAsync();
	};
    categories.LoadAsync(_context.Categories);
</code>

The LoadAsync method takes an IQueryable parameter, which allows you to write LINQ queries to sort
and filter the data. For example, the code below populates the Products grid in the sample with 
products that match the category selected in the Categories grid:

<code>
    void _flexCategories_SelectionChanged(object sender, C1.Silverlight.FlexGrid.CellRangeEventArgs e)
    {
        // get selected category
        var category = _flexCategories.SelectedItem as Category;
        if (category != null)
        {
            // build query to select products in the selected category
            var query = from p in _context.Products
                    where p.CategoryID == category.CategoryID
                    orderby p.ProductName
                    select p;

            // assign new products collection to grid
            var products = new DataServiceCollection<Product>();
            _flexProducts.ItemsSource = products;

            // load products in the selected category
            products.LoadAsync(query);
            products.LoadCompleted += (s, ee) =>
                {
                    if (products.Continuation != null)
                    {
                        products.LoadNextPartialSetAsync();
                    }
                };
        }
    }
</code>

NOTES:

1) The sample was written to illustrate how to use OData web services in Windows Store applications.
It could be optimized by caching some product lists or in this case even loading the entire products 
table and then applying filters to the bottom grid's CollectionView.