Introduction
This article explains how to use the Query Operators in Web API2 for filtering the data. For this we need to make the Web API method a Queryable method. Here we use the OData library that provides the [Queryable] attribute to the ASP.NET Web API2.
The following are the various query methods that we can use in the Web API:
- $top=n: Here n=1,2,3,..... it returns the first n entities from the entity set.
- $skip=n: Here n=1,2,3,4..... it skips the first n entities from the entity and returns the rest of the entities.
- $select: It returns only the specified properties of the entity.
- $filter: It returns only that entities that are matched with the expressions.
- $orderedby: It returns the result in descending or ascending order. That is the Value of the properties.
- $format: It defines that the returning data in which format such as XML or JSON.
Use the following procedure to create the Web API application.
Step 1
Create the application using the following:
- Start Visual Studio 2013.
- From the Start Window select "New Project".
- Select "Installed" -> "Template" -> "Visual C#" -> "Web" -> "Visual Studio 2012" and then select "ASP.NET MVC4 Web Application".

- Click on the "OK" button.
- From the MVC4 project window select "Web API".

- Click on the "OK" button.
Step 2
Now add the Model Class:
- In the Solution Explorer.
- Right-click on the Model folder.
- Select "Add" -> "Class".
- Select "Installed" -> "Visual C#" and select "Class".

- Click on the "Add" button.
Add the following simple lines of code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace WebApplication15.Models
- {
- public class Employee
- {
- public int id { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- }
- }
Step 3
Now add an API Controller to the project as in the following:








Join the conversation! Your thoughts help the community grow.