Powering LINQ and joins over OData

Joins over Odata

–          Joins Categories and products table/vategory with the combination of categoryid and product id

–          Get the collection/records of Categories and products table/category based on the condition

–          Get only selected field/column value based on the join condition

–          Get selected filed/column value of the Products table/category based on joining condition

–          Get only a value string (with no xml tags) of selected field/column value based on joining condition

–          Get the collection/records linked with products table/category

–          Works same as the above query which identifies the category table/category and gets the collection/records associated with products table

–          Get selected fields/entity of all the collection/records of the products table/category associated with categories table/category

–          Get selected fields/entity of the first collection/record of products table/category associated with categories and order_details table/category

–           Get selected filed/column value of the Products table/category based on joining and search condition in which OrderID=10285

–          Get selected filed/column value of the Products table/category based on joining and search condition in which price greater than 200

Odata formats:

–          Get the collection/records of the products table/category with atom format (default fomat)

–          Get the collection/records of the products table/category with json format

For more information please click here

With all these information hope you get a good idea on OData and in another corner you might be scratching your head thinking all the queries are written only through url (http context) but how to use this in .net code-behind with LINQ.

We can also use the http context call in code-behind but obviously we can use our OData web service with LINQ in code-behind.

Here we go with the process.

Add a web page to your web project. Add a web service reference which you have created earlier to your project. Then write the code in your web page code behind file as follows

var serviceURI = new Uri("http://www.yourserviceurl.com/WcfDataService1.svc");
var context = new NorthwindService.NORTHWNDEntities(serviceURI);

//Get all records/collection of the products table/category
var query = (from el in context.Products select el).toList();

//Get the top record/collection of products table/category
var query = (from el in context.Products select el).FirstOrDefault();

//Get all top records/collection of the products table/category by joining category, supplier and order_details table/category
var query = (from el in context.Products.Expand("Category,Supplier,Order_Details") where el.ProductID.Equals(1) select el).FirstOrDefault();

//Search the result collection of the last quesry with orderID
var searchres = (from el in query.Order_Details where el.OrderID.Equals(10285) select el).FirstOrDefault();

Once you get the required collection with the join you can search and do other linq operations. Please feel free to contact me @Raghav for any queries

Basic queries of Odata

Some basic queries to play around with Odata

–          Get all the collection/records of the Products category

–          Get a single entity/record of the products with the value productid 1

–          Get only productName filed of entity/record of the products category with the value of productid 1

–          Get the collection/records count of the products category

–          Get all the collection/records of the Products category order by rating field/column/entity ascending

–          Get all the collection/records of the Products category order by rating field/column/entity descending

– Passing a value ‘car’ to the function ‘ProductsByType’ which accepts a single string parameter

–          Get the top 2 collection/records of the Products category

–          Skip first 5 records and get all collection/records of the products category

Serving OData

Odata is open data web protocol for querying and updating data. The main advantage avail is reduced bandwidth. We can query or update only as much data as we want.

We all know that more than 50% of the internet users are using wireless devices to access internet nowadays. Expectations are high with speed and performance when using any web apps or web sites. So Odata is a right techi to adopt and its right time.

OData is very powerful and efficient for accessing data faster. Performance is the key. Since it works with https context and querying the data is easy and more flexible. Odata says, “I give everything, you take how much you want” – on a single hit.

So a vendor can expose any bulk of data over the web service but customer can query and get only as much data as they need. This reduces bandwidth and avoids downloading unnecessary data.

Let us setup a web service to work with Odata:

  • Start Visual Studio 2010 and select new ASP.net web project
  • Add new ADO.NET Entity Data Model (.edmx) item and name it as “Northwind”
  • Select generate from database, select SQL server, choose connection and add selected tables to .edmx file.
  • Add new WCFDataService file and add the following code
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace Test
{
// Add the class name of your edmx file to your DataService and set the entity access rule here.
public class WcfDataService1 : DataService
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
}
  • Run the web service. Add the service reference to the Web project.

Now the application is ready with Odata webservice.

Run the service with .net or host the service in IIS and your service is ready to query through http context.